RHEL : A better cheat Sheet
Tags: devops, productivity, linux, tutorial
Published: 2024-05-11 21:39:03

#### contents - 
#### :[1.Know your machine 💻](#know)
#### :[2.Monitoring 🔧](#monitoring)
#### :[3.Tweaks for better Administration 👨🏻‍💻](#tweaks)
#### :[4.Networking 📡](#networking)
#### :[5.Searching 🔍](#searching)
#### :[6.Automation ⚙️](#automation)
#### :[6.RHEL Web Console 👾](#console)
#### :[7.File Exploring 📁](#exploring) 
**--------------------------------------------**

###1. Know your machine 🖥🖥️


#### 1.Display Linux system information-
###`uname -a`
###`hostnamectl`

#### 2.Show operating version & name-
### `cat /etc/os-release`

![os version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yy2btmjr1fvghvhlpnzc.png)

#### 3.Show host name-
### `hostname`
### `hostname -I`

#### 3.Show users currently logged in- W
### `w`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dr3tpiyj0gox6loxqhri.png)



#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eifarpjxguaepyjjn8s.png)

#### 12. List installed packages- 
yum list installed

---
#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fslolkmyte40jw42n7o.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvtpcj6n68fb6fgqmypi.png)
### `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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e5qbw8el1ltb6o5jqu0.png)

Show mountpoints in tree-
###`findmnt`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23c2o5no60zddaufz255.png)

###`lsblk`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iihtzw5i7gejmip9udfw.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sporudjrtd3ql09doonq.png)


To view files in tar- 
`tar -tvf file_name.tar.gz`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbx68d2lenh1espp9qk8.png)

To extract files- 
`tar -xzvf file_name.tar.gz`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d01rjpdptjy12xbq04j5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovslpdmb87cr343a4bpi.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i34tlqtdtcjlr7yz42kc.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0fmv71bnckuenfkqcrw.png)




---
#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ywrrc4yvjjj2eemks2q.png)

![recorded Session](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u89m29j9224dek6oyxqj.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmpb8yeqxg4m0v6b0g32.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7pll3u8hztyvztl1h9b.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tblt7sep9hmgs9syizw.png)


To send msg to a specific user group- 
`wall -g [group-name] [message-text]`

---
#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldptjwvar8xgunm0rgut.png)

connection failure - 
![fail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg1ezt6fynjmwuzz3a24.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s031o05kuawhujg6o5mo.png)



#### 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
---
#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`

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

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

###3. SED -
`sed OPTIONS... [SCRIPT] [INPUTFILE]` 

Examples-
replace a string on one or more files
`sed -i 's#ORIGINAL_VALLUE#NEW_VALUE#g' myfile1 myfile2`
``
---

#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5ro0o9yz71bfwodv4ry.png)

System Overview-
![Overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yeuy9vvoz4wn66g588x8.png)

Install tools - 
![Install software](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htv9wk2h927z0f34pa4i.png)

Critical Logs -
![logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq8g9lw9ho05xvlx9v03.png)
Terminal access - 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4naozxcuyrcax102d3ko.png)



Storage-
![Storage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftd86v10jo6whty5xr8p.png)

Network details- 
![Network details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6eydw97lu21ffb57f8r.png)

users & groups
![users & groups](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mo7envu2dyg99ylxe3b.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqaxy71iu7m0sy6lztb2.png)