Cheat Sheet for Linux Admin Starters

Cheat Sheet for Linux Admin Starters

The below are the most important Linux commands that is needed for Linux Adminstrators on their day2day work.

Vi Cheat Sheet

:set smd                          // Show Current mode "Command / Edit / Append".
:%s/the/THE/g                     // Substitute the by THE globally
:%s/the/THE/gc                    // Substitute the by THE globally with Checking
A                                 // Append at the end of the line.
I                                 // Write at the start of the line
u                                 // Undo last action command
U                                 // Undo last action command
dw                                // Delete Word
3dw                               // Delete 3 Words
D                                 // Delete from cursor till the end
Ctrl + l                          // Refresh Screen
p                                 // Paste dd lines
.                                 // Redo last action
yy                                // Copy Lines
R                                 // Replace Mode
~                                 // Toggle Upper and Lower case.

The below Command is used only when the list is too big to be deleted via the ordinary rm command

find . -name '20170504*' -exec rm {} \;

Hanged Process

truss -p PID &

Some important find commands

find . -name 'A2009*' -print;
find . -type f -print;                    // File
find . -type d -print;                    // Directory
find . -type c -print;                    // 
find . -size 4 -print;                    // Size by Block
find . -size +4 -print;                   // Size by Block greater than 4
find . -user dwsadm -print;               // owner dwsadm
find . -group dwsadm -print;              // group dwsadm
find . -mtime 4 -print;                // modification time by days
find . -atime +4 -print;               // Access time
find . -ctime +4 -print;               // Creation time
find . -inum 337 -print;               // Search by inode num
find . -name 'mina*' -exec rm {} \;     // Force execute.
find . -name 'mina*' -ok rm {} \;       // interactively prompt.

Grepping with line numbers

grep -n sqlplus /home/scripts/*          // grepping with numbering

Redirection to both Stdout and to file.

cat config.txt |sort > Sorted.txt |tee Sorted.txt

Disable Redirection Commands

set -o noclobber     // Disable redirection for all current sessions
set +o noclobber    // Enable redirection for all current sessions

Compressing Commands

tar cvf /tmp/backup.tar *    // Tape Achieving and placing the output in /tmp/backup.tar 
jar cvf /tmp/backup.tar *

tar czf ones.tar.gz 1_*.txt
tar czf twos.tar.gz 2_*.txt

Identify the port being used by a process in Linux

lsof -Pan -p 4295 -i

Get the sum of a column using awk

echo "$1" |awk '{sum += $1} END {print sum}'

Get Some historical metrics from the system

Historical CPU usage

[user@host ~]$ sar -p

Historical memory usage

[user@host ~]$ sar -r
[user@host ~]$ sar -S

Historical disk I/O usage

[user@host ~]$ sar -d

Debugging Bash Scripts

bash -x script1.sh

Disk Space Analysis

[intersec@vito-web-02 one]$ du -sh *
28K     core
16G     master

Group By and sum using awk

20180526T00:03:18,2196909
20180526T00:01:55,33147
20180526T00:02:52,23067
20180526T00:01:52,78984
20180526T00:01:11,443759
20180526T00:03:27,8735
20180526T00:01:21,433750
20180526T00:01:20,4454722
20180526T00:03:14,38854


awk -F"," '{split($1,a,":");sum[a[1]]+=$2}END{for(pat in sum){print pat","sum[pat]/1099511627776}}' Data_Trend_time_volume

20180527T22,5.3363e-06
20180527T04,0.00328853
20180527T23,0.000149725
20180527T15,0.00328
20180527T00,0.00219587

Some ps tricks

ps -fu dwsadm       // full details for process for user dwsadm

[root@Oct_P-sc-1 ~]# sleep 100
^Z              (CTRL + Z)
[1]+  Stopped                 sleep 100
[root@Oct_P-sc-1 ~]# bg 1
[1]+ sleep 100 &
[root@Oct_P-sc-1 ~]# 
[root@Oct_P-sc-1 ~]# jobs
[1]+  Running                 sleep 100 &
[root@Oct_P-sc-1 ~]# disown -h %1

Curl Use this command to test an application's endpoint or connectivity to an upstream service endpoint. curl can be useful for determining if your application can reach another service, such as a database, or checking if your service is healthy.

As an example, imagine your application throws an HTTP 500 error indicating it can't reach a MongoDB database:

$ curl -I -s myapplication:5000
HTTP/1.0 500 INTERNAL SERVER ERROR

Environmental Variables env allows you to set or print the environment variables. During troubleshooting, you may find it useful for checking if the wrong environment variable prevents your application from starting.

env
PYTHON_PIP_VERSION=9.0.1
HOME=/root
DB_NAME=test
PATH=/usr/local/bin:/usr/local/sbin
LANG=C.UTF-8
PYTHON_VERSION=3.4.6
PWD=/
DB_URI=mongodb://database:27017/test

netstat

netstat shows the network status. This command shows network ports in use and their incoming connections

netstat -tulpn

nslookup A domain name server (DNS) helps resolve a URL to a set of application servers. However, you may find that a URL does not resolve, which causes a connectivity issue for your application

nslookup mydatabase
Server:   10.0.2.3
Address:  10.0.2.3#53

** server can't find mydatabase: NXDOMAIN

iptables iptables blocks or allows traffic on a Linux host, similar to a network firewall. This tool may prevent certain applications from receiving or transmitting requests.

$ iptables -S
-P INPUT DROP
-P FORWARD DROP
-P OUTPUT DROP
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p udp -m udp --sport 53 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT
-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT

SELinux

sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

Other useful commands

  • If you faced some limitations in the normal awk command try using gawk or nawk
  • lsmod command displays a module's status within the kernel, which helps troubleshoot server function issues.
  • od command dumps binary files in octal -- or hex/binary -- format to standard output
  • rsync command data from one disk or file to another across a network connection. It is similar to rcp, but has more options
  • sdiff command to compare two files and produce a side-by-side listing indicating lines that are dissimilar.