DEV Community

Cover image for Mastering RHEL Linux: A Comprehensive Cheat Sheet for Beginners & Experts
kaustubh yerkade
kaustubh yerkade

Posted on • Updated on

Mastering RHEL Linux: A Comprehensive Cheat Sheet for Beginners & Experts

contents -

:1.Know your machine ๐Ÿ’ป

:2.Monitoring ๐Ÿ”ง

:3.Tweaks for better Administration ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป

:4.Networking ๐Ÿ“ก

:5.Searching ๐Ÿ”

:6.Automation โš™๏ธ

:7.RHEL Web Console ๐Ÿ‘พ

:8.File Exploring ๐Ÿ“

:9.VI Editor ๐Ÿ“

--------------------------------------------

1. Know your machine ๐Ÿ–ฅ๐Ÿ–ฅ๏ธ

1.Display Linux system information-

uname -a

hostnamectl

2.Show operating version & name-

cat /etc/os-release

os version

3.Show host name-

hostname

hostname -I

(to change hostname- $sudo hostname new_hostname)

3.Show users currently logged in- W

w

Image description

4.Display CPU information-

cat /proc/cpuinfo

5.Display memory/RAM -

cat /proc/meminfo

6.Display RAM usage (redable, in MBs, in GBs)

free -h

free -m

free -g

7.Displays all environment variables running on the system.

env

8.Display DMI/SMBIOS (hardware info) from the BIOS

dmidecode

9.To List all installed packages on machine -

rpm -qa

10.To list active services -

systemctl list-units --type=service

11.Displays kernel-related messages

dmesg -HTx

Image description

12. List installed packages-

yum list installed

13. To remove a package in RHEL using yum, you can use the following commands:

yum remove [package_name] : Removes a package by its name
yum remove-n [package_name] : Removes a package by its exact name
yum remove-na [package_name].[architecture] : Removes a package by its exact name and architecture
yum remove-nevra [package_name]-[epoch]:[version]-[release].[architecture] : Removes a package by its exact name, epoch, version, release, and architecture
yum autoremove [package_name] : Removes a package and all its unneeded dependencies


monitoring

To check running processes with more details-

ps aux

To get PID of running processes by name-

pgrep process_name

lists all active services managed by systemd

systemctl list-units --type=service

1.WATCH - shows periodic updates in output

watch df -h

watch -n 5 -d '/bin/free -m'

Image description
Monitor open netstat connections-
watch -n 1 "netstat -tpanl | grep ESTABLISHED

2.Monitor all traffic on port

tcpdump -i device 'port port_n0'

tcpdump -i enp0s3 'port 9090'

3.Display the last 100 syslog messages

tail -100 /var/log/messages

head -100 /var/log/messages

4.reboot History

last reboot

5.History - to check previous commands.

history 100

6.NMON - 'TOP' alternative for monitering-

nmon screen

nmon

NMON can be used as a better 'top' alternative.

1.CPU utilization,
2.Memory
3.Disks
4.Network utilization & more
In a better viewe where refresh rate can be adjusted by pressing +, - buttons.
Stats can be saved to CSV for later analysis & graphing.


exploring

File Exploring

List only directories-

ls -d */

List files with subdirectories-

ls *

Image description

Show mountpoints in tree-

findmnt

Image description

lsblk

Image description

List files recursively-

ls -R

List files with their sizes-

ls -s

List files in long format-

ls -lrt

List files in long format with readable file sizes-

ls -lh

List files including hidden files-

ls -a

List files and sort by file size-

ls -S

-t <- time

-r <- reverse

compare files - diff | comm | cmp

to check difference in 2 files-

diff file1 file2

to check difference by lines & size-

cmp file1 file2

to display unique lines in 2 files-

comm file1 file2

to check size of current directory-

du -sh

to find the largest log file-

find / -type f -name "*log*" | xargs ls -lSh | more

rsync - scp alternative. For copying and synchronizing files and directories remotely and locally, Can be used to mirror data on 2 machines.-

rsync [OPTIONS] /SOURCE /DESTINATION

-v -display transfer details
-P โ€“displaying detailed information
-r โ€“copies data recursively
-a โ€“copies data & preserves file permissions, user & group ownerships,
and timestamps.
-z โ€“Compress files during transfer to reduce network usage.
-h โ€“output transfer numbers in a human-readable format.

Copying files

cp -r source_folder /path/to/destination_folder/

TAR - compress & Extract files -
To compress files-

tar -czvf file_name.tar.gz *

Image description

To view files in tar-

tar -tvf file_name.tar.gz

Image description

To extract files-

tar -xzvf file_name.tar.gz

Image description

MC - Midnight Commander - To manage files grahically on terminal-
can be managed using input from mouse. can be used for- Copy, Delete, Rename , Move, make Directory , edit files & change permissions. Press TAB to swich between the panes of active directories. Bottom menu can be accessed using Function keys F1-F10.

mc

Image description

Image description

Image description

Check out : VI Editor tips & Tricks


tweaks

for better Administration

1.Record Terminal session -

to start recording session-

script script_name.txt

to stop session-

exit

to view the recorded session-

cat script_name.txt

script start

recorded Session

2.Increase SSH timeout -

vi /etc/ssh/sshd_config
insert/assign values as per following to set timeout to 1 hour -

ClientAliveInterval 1200
ClientAliveCountMax 3

3. long running command in background -

long running commands can be sent to background by simply using '&' after the command. Ampersand instructs the shell to execute the command as a separate background process.
for e.g checking unreadable blocks on disk took hours to get finished, So this job will be sent to background-
badblocks -s /dev/sda &
to check status - jobs

4. nohup

nohup is extension to '&' , is used to keep the process running in background even after user logs out,

nohup your_command &

e.g. nohup sh script_runner.sh &
to check status - jobs

5. TMUX -

to enable second terminal pane for multitasking without opening a second session.

  1. Type tmux
  2. To open second pane - Ctrl + b then Shift + 5(%)
  3. To switch pane - Ctrl + b then left arrow or right arrow to switch the curser.
  4. To close pane - Ctrl + b followed by d
  5. type tmux attach to open the earlier pane. (after closing the pane session continues running in bg)

Image description

Check byobu , screen & Zellij for more enhanced terminal multiplexing.

Image description

Check out : VI Editor tips & Tricks

6. DISOWN -

disown command removes the given process from running terminal shell & runs in background until completion. this can be used for long running jobs on time limited sessions.

Image description

7. to save command output in a file-

your_command > file1.txt

to append more commands on same file -

your_2nd_command >> file1.txt

8. Solving space crunch -

1.find largest file on server by-

find / -type f -name "*log*" | xargs ls -lSh | more

2.empty the file using echo trick instead of using rm-

echo "" > access.log

9. wall - Send a msg to all logged in users -

wall -n hi all, system reboot will be initiated in 10 mins

Image description

To send msg to a specific user group-

wall -g [group-name] [message-text]

9.To find recent errors in system using journalctl-

journalctl --no-pager --since today \ --grep 'fail|error|fatal' --output json|jq '._EXE' | \ sort | uniq -c | sort --numeric --reverse --key 1
Enter fullscreen mode Exit fullscreen mode

10.Search specific files fast-

grep -R 'import' --include='*.java' --color MySourceCodeDir

11.cfg2html- bash script

A powerfull bash script Useful in recovery like situations, exports necessary system configuration files and system setup in html & txt format.

git clone https://github.com/cfg2html/cfg2html

./cfg2html

Image description

Image description

12. BCC (BPF Compiler Collection)

BCC is a toolkit for creating efficient kernel tracing and manipulation programs, and includes several useful tools and examples. It makes use of extended BPF (Berkeley Packet Filters),
url - https://github.com/iovisor/bcc

Image description

13. Log Everyone Out Of The System

use 'w' to check who are currently logged in.

Following root command will forcefully log out the user -

pkill -KILL -u user_name


networking

1.Monitor all traffic on port

tcpdump -i device 'port port_n0'

tcpdump -i enp0s3 'port 9090'

To check open ports on server-

netstat -pnltu

netstat -nutlp

Check connectivity & port opening status

ssh -vvv <IP> -p <PORT>

ssh -vvv 192.168.186.42 -p 9090

connection succesful -
successful

connection failure -
fail

NetCat -

nc -z -v -w 5 <IP> <PORT>

Image description

To get DNS information-

dig dns_name

Display the top 10 IP addresses hitting a webserver -

cat /var/log/nginx/access.log | cut -f 1 -d ' ' | sort | \ uniq -c | sort -hr | head -n 10

To change hostname -

sudo hostname new_name

To create ssh tunnle

ssh -f -L 9000:TARGET_SERVER_IP:8088 root@IP_ADDR -N

To Print routing-

traceroute google.com

nc -vw5 google.com 80

Sniff network traffic on a network interface-

sudo tcpdump -i wlan0 -n ip | awk '{ print gensub(/(.*)\..*/,"\\1","g",$3), $4, gensub(/(.*)\..*/,"\\1","g",$5) }' | awk -F " > " '{ print $1" "$2}'


searching

1.AWK- data extraction from files, text processing, report generation

Syntax-
awk options 'selection _criteria {action }' input-file > output-file

Examples -
1.Sorted print - login name of all users-

awk -F ":" '{ print $1 | "sort" }' /etc/passwd

2.Calculate size of directory-

ls -al | awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'

3.To count size of specif files in dir-

ls -l *.sh *.yaml | awk '{sum+=$5} END {print sum}'

4.Print all lines of a file prefixed with a line number-

awk '{print NR, $0}' [FILENAME]

5.To extract e-mail address from a file-

awk '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/ { print }' file.txt

6.To extract FQDN,IP, URL from a log file-

awk '$6~/GET/{split($7,url,"/"); print url[3]}' /App_logs/log/sysout.log

7.To print error lines-

awk '$3 == "Error:"' /App_logs/log/sysout.log

2. GREP- Global Regular Expression Print - To look for things in files

Syntax-
grep [options] pattern [files]

Examples-
1.Search for lines matching 'pattern' in file1.txt-
grep 'pattern' file.txt

2.Case-insensitive search for 'pattern'-
grep -i 'pattern' file.txt

3.Print lines not containing 'pattern'-
grep -v 'pattern' file.txt

4.Count lines containing 'pattern'-
grep -c 'pattern' file.txt :

5.Print lines with line numbers-
grep -n 'pattern' file.txt

6.Recursively search in directory dir/
grep -r 'pattern' dir/

7.Highlight the matched pattern-
grep --color 'pattern' file.txt

8.Extended regex for multiple patterns-
grep -E 'pattern1|pattern2' file.txt

9.List files containing 'pattern'-
grep -1 'pattern' dir/*

10.Match whole words only-
grep -w 'pattern' file.txt

11.Print only the matched parts of the line-
grep -o 'pattern' file.txt

12.Print 3 lines after the matched line-
grep -A 3 'pattern' file.txt

13.Print 3 lines before the matched line-
grep -B 3 'pattern' file.txt:

14.Print 3 lines around the matched line-
grep -C 3 'pattern' file.txt

15.Use patterns from file for matching-
grep -f patterns.txt file.txt

16.Search only in .txt files within dir/-
grep --include '*.txt' 'pattern' dir/

17.Exclude log files from search-
grep --exclude '*.log' 'pattern' dir/

18.Quiet mode, returns 0 if pattern is found-
grep -q 'pattern' file.txt

19.Use pattern for matching-
grep -e 'pattern' file.txt

  1. Print details about processes- prints all processes of java ps -ef | grep java prints all DBs currently running ps -ef| grep pmon

21.Recursively search with symbolic links-
grep 'pattern' -R dir/

3. SED- Stream Editor for parsing and transforming text

sed (Stream Editor) is a powerful text-processing tool in Linux/Unix for parsing and transforming text in files or streams.
sed Flags:

  • s: Substitute.
  • g: Global replacement (all occurrences).
  • i: Case-insensitive matching.
  • d: Delete.
  • -i: Edit in place.
  • -n: Suppress automatic output.

Syntax-
sed OPTIONS... [SCRIPT] [INPUTFILE]

Examples-

  1. Basic Search and Replace- Syntax: sed 's/old_text/new_text/' file Example: Replace the first occurrence of "apple" with "orange" in each line of a file:

sed 's/apple/orange/' file.txt

  1. Global Search and Replace (All Occurrences in Each Line)- Syntax: sed 's/old_text/new_text/g' file Example: Replace all occurrences of "apple" with "orange":

sed 's/apple/orange/g' file.txt

  1. Edit a File in Place- Syntax: sed -i 's/old_text/new_text/g' file

Example: Replace all occurrences of "apple" with "orange" and modify the file directly:

sed -i 's/apple/orange/g' file.txt

The -i option edits the file in place, meaning it overwrites the original file.

  1. Replace Only on a Specific Line- Syntax: sed 'line_number s/old_text/new_text/' file Example: Replace "apple" with "orange" on line 3 only:

sed '3 s/apple/orange/' file.txt

  1. Replace Between Line Ranges Syntax: sed 'line1,line2 s/old_text/new_text/g' file Example: Replace all occurrences of "apple" with "orange" between lines 2 and 5:

sed '2,5 s/apple/orange/g' file.txt

  1. Delete Lines Containing a String Syntax: sed '/pattern/d' file Example: Delete all lines containing "apple":

sed '/apple/d' file.txt

  1. Delete Specific Line- Syntax: sed 'line_number d' file Example: Delete the 2nd line of the file:

sed '2d' file.txt

  1. Insert Text Before a Line- Syntax: sed 'line_number i\text_to_insert' file Example: Insert "Hello, World!" before line 3:

sed '3i\Hello, World!' file.txt

  1. Append Text After a Line- Syntax: sed 'line_number a\text_to_append' file Example: Append "End of section" after line 4:

sed '4a\End of section' file.txt

  1. Substitute with a Regular Expression- Syntax: sed 's/regex/replacement/' file Example: Replace any digit with a "#" symbol:

sed 's/[0-9]/#/g' file.txt

  1. Replace a Word Only If Another Word Exists on the Same Line- Syntax: sed '/pattern1/s/pattern2/replacement/' file Example: Replace "orange" with "banana" only on lines that also contain "apple":

sed '/apple/s/orange/banana/' file.txt

  1. Print Specific Lines- Syntax: sed -n 'line_number p' file Example: Print only the 2nd line of the file:

sed -n '2p' file.txt

To print multiple lines (e.g., lines 2 to 5):

sed -n '2,5p' file.txt

  1. Replace a Delimiter in a File- If the delimiter in a CSV file is a comma, you can replace it with a semicolon:

sed 's/,/;/g' file.csv

  1. Remove Empty Lines- Syntax: sed '/^$/d' file Example: Delete all empty lines from a file:

sed '/^$/d' file.txt

  1. Remove Leading and Trailing Whitespace- Syntax: sed 's/^[ \t]//;s/[ \t]$//' file Example: Remove leading and trailing spaces:

sed 's/^[ \t]*//;s/[ \t]*$//' file.txt

  1. Replace Multiple Spaces with a Single Space- Syntax: sed 's/ */ /g' file Example: Collapse multiple spaces into a single space:

sed 's/ */ /g' file.txt

  1. Case-Insensitive Search and Replace- Syntax: sed 's/old_text/new_text/I' file Example: Replace "apple" with "orange", case-insensitive:

sed 's/apple/orange/I' file.txt

  1. Backup Files When Editing In-Place- Syntax: sed -i.bak 's/old_text/new_text/g' file Example: Replace "apple" with "orange" and create a backup of the original file as file.bak:

sed -i.bak 's/apple/orange/g' file.txt

Image description

Check out : VI Editor tips & Tricks


console

RHEL Web Console-
A web-based interface can be used for managing and monitoring RHEL systems in realtime. Follow the commands to enable & use RHEL web console-

Enable web console -
systemctl enable --now cockpit.socket

if cockpit package not installed -
dnf install cockpit

open port 9090 for web console -
firewall-cmd --add-service=cockpit --permanent

open browser & type URL -
https://IP or hostname:9090
e.g. https://192.168.186.42:9090/system

login page-
login page

System Overview-
Overview

Install tools -
Install software

Critical Logs -
logs
Terminal access -

Image description

Storage-
Storage

Network details-
Network details

users & groups
users & groups

Image description

[------

Automation

1.at command - an easier alternative for cron.
Task scheduling for shorter times or to automate simpler jobs.

at

echo "hello world" | at 1:30 AM

To view at schedule-

atq

To remove a job-

atrm job_number

Time expressions for at-

  • now
  • midnight
  • noon
  • teatime (4 PM)
  • AM
  • PM
  • minutes
  • hours
  • days
  • weeks
  • months
  • years

Examples-

echo "rsync -av /source/files /destination" | at 3:30 AM tomorrow

echo "mv filename filename2" | at 3:30 AM 08/01/2022

echo "./script_runme.sh" | at now + 3 days


2.CRON

crontab is used to schedule recurring tasks at specified times or intervals. These tasks, often called cron jobs, are managed by the cron daemon (crond), and can automate various tasks like backups, system maintenance, or running scripts.

Cron format

Image description

Cron time examples

Image description

Crontab Special Strings
Crontab Special Strings are predefined shortcuts in the crontab that simplify scheduling common time intervals. Instead of specifying the exact minute, hour, day, month, and weekday, you can use these strings to save time when defining common schedules.

@reboot : Runs once, at startup.
@hourly : Runs once per hour.
@daily : Runs once per day (midnight).
@weekly : Runs once per week.
@monthly : Runs once per month.
@yearly : Runs once per year.
@annually : Runs once per year.

Examples -

1.Run a script at system reboot:
@reboot /home/user/script.sh

2.Run a backup script every month:
@monthly /home/user/backup.sh

3.Run a maintenance task every week:
@weekly /home/user/maintenance.sh

4.Run a log cleanup every day at midnight:
@daily /home/user/log_cleanup.sh

5.Run a maintenance task every Year:
@yearly /home/user/yearly_script.sh

cron to execute a script and save output to a log file: To capture both the output and errors:

* * * * * /path/to/script.sh >> /path/to/output.log 2>&1

'>>'Appends output to a log file.
'>' Overwrites the log file.
'2>&1' Combines both standard output and error into the same log file.
Use $(date +\%Y-\%m-\%d) for timestamped log filenames.

Example with Timestamped Logs , If you want to save the output in a file with a timestamp for each run-

* * * * * /path/to/script.sh >> /path/to/logs/output_$(date +\%Y-\%m-\%d_\%H:\%M:\%S).log 2>&1

The following cron will run the backup.sh script every day at 3 AM and save the output (including any errors) in a log file named backup_YYYY-MM-DD.log-

0 3 * * * /home/user/backup.sh >> /home/user/logs/backup_$(date +\%Y-\%m-\%d).log 2>&1

Top comments (6)

Collapse
 
cyberceelo profile image
Carlos Melo

THANK YOU , made this account to to let you know this helped a lot for me.

Cheers !

Collapse
 
samidha_yerkade_541d4efd7 profile image
Sam_says#

Great article @kaustubhyerkade !!
VI is greatly explained , using command mode is little confusing but now my search stops here

Collapse
 
samidha_yerkade_541d4efd7 profile image
Info Comment hidden by post author - thread only accessible via permalink
Sam_says#

Great article @kaustubhyerkade !!
VI is greatly explained , using command mode is little confusing but now my search stops here

Collapse
 
kos_mos_6e83a04829ada1fdd profile image
Vivek Parag • Edited

Been into linux administration since ages, still some of these commands are new for me. thanks for sharing..

Collapse
 
farheen_sk profile image
Farheen Shaikh

Great job @kaustubhyerkade !

Collapse
 
web_devlopers_cf6d524353e profile image
web devlopers

RHEL 9's web console is the best.

Some comments have been hidden by the post's author - find out more