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
3.Show host name-
hostname
hostname -I
(to change hostname- $sudo hostname new_hostname)
3.Show users currently logged in- W
w
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
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'
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
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 *
Show mountpoints in tree-
findmnt
lsblk
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 *
To view files in tar-
tar -tvf file_name.tar.gz
To extract files-
tar -xzvf file_name.tar.gz
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
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
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.
- Type
tmux
- To open second pane - Ctrl + b then Shift + 5(%)
- To switch pane - Ctrl + b then left arrow or right arrow to switch the curser.
- To close pane - Ctrl + b followed by d
- type
tmux attach
to open the earlier pane. (after closing the pane session continues running in bg)
Check byobu , screen & Zellij for more enhanced terminal multiplexing.
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.
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
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
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
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
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
NetCat -
nc -z -v -w 5 <IP> <PORT>
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
20.Recursively search with symbolic links-
grep 'pattern' -R dir/
- Print details about processes-
ps -ef | grep java
prints all processes of javaps -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
``
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
Critical Logs -
Terminal access -
[------
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-