You are on page 1of 4

Linux Commands to know by DevOps Engineer Cheat Sheet

by rakesh zingade via cheatography.com/71377/cs/18096/

System/OS related commands User admin Commands

To know the OS type: To know the CPU To know the group/user Check user added or not into system:
$ uname -o archit​ect​ure: exists on the system: $ id <us​ern​ame>
$ uname -m $ getent group e.g. $ id cloudu​ser1

To check the kernel version: To get the OS name, release, <group name>

$ uname -r version: $ getent passwd

$ cat /etc/o​s-r​elease <user name>

To list the system hardwa​re: To get the CPU details: To create a new group: Modify existing user, add user to group:

$ lshw $ lscpu $ sudo groupadd $ sudo usermod -aG <group name>


<group name> <us​ern​ame>
To check system memory: To check the virtual memory
e.g. $ sudo e.g. $ sudo usermod –aG sudo
$ free -h stats:
groupadd training cloudu​ser1
$ vmstat -S m

Free memory cache, dentries and To print the process specific To delete the existing Add user’s home directory (example for

inode (with root): $ echo 3 > memory utiliz​ati​ons: group: cloudu​ser​1):


$ sudo groupdel $ sudo mkdir -p /home/​user1
/proc/​sys​/vm​/dr​op_​caches $ ps aux --sort​=-%mem
<group name> $ sudo chown
To search packages for instal​lat​ion: To installed package:
e.g. $ sudo cloudu​ser​1:c​lou​duser1
$ apt search <pa​ckage name> $ sudo apt-get install
groupdel training /home/​user1 $ ls -l /home
e.g.: <pa​ckage name>
drwxr-xr-x 2 clouduser1
$ apt search python​-boto
clouduser1 4096 Nov 18 12:13
To uninstall package: To list the mounted disk user1
$ sudo apt-get remove <pa​ckage name drives: $ sudo usermod -d /home/​user1
$ df -kh
clouduser1
To mount the volume: To list biggest files from $ id clouduser1
(create the directory first to mount directory (biggest 5): uid=10​02(​clo​udu​ser1)
volume) $ sudo du -a /dir/ | gid=10​03(​clo​udu​ser1)
$ mkdir -p <di​rectory path sort -n -r | head -n 5 groups​=10​03(​clo​udu​ser​1),​27(​sudo)
e..g /mount​-vo​l>
$ su - clouduser1
$ sudo mount <src path>
$ pwd
<above created dir path>
/home/​user1
Find the file (search for a file): Search the text string in a
Print the groups to Delete existing user with all files
$ find <dir path> -name directory and print filename
which the current user associated with user:
<fi​len​ame> -print containing that string:
is associ​ated: $ sudo userdel -r clouduser1
e.g. to find app.log in /var $ file /var -type f -
$ groups $ id clouduser1
directory print | xargs grep
id: ‘cloud​user1’: no such user
$ find /var -name app.log –print` <search text>

File the text string from a given direct​ory:


$ grep -rIn <search text> <di​rectory path>

By rakesh zingade Not published yet. Sponsored by Readability-Score.com


cheatography.com/rakesh- Last updated 2nd December, 2018. Measure your website readability!
zingade/ Page 1 of 4. https://readability-score.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/

User admin Commands (cont) Editor​/Text manipu​lation commands (cont)

Change the group name: Add user to system: cut, cutting out the sections Uniq, is a command that filter
$ sudo groupmod –n <new group name> $ sudo adduser from lines: out the duplic​ates
<old group name> <user name> $ cut -d “delim​iter” -f a) fetch

e.g. I want to change the groupname e.g. add <fi​eld> <fi​le.t​xt> repeat​ed/​dup​licate lines

‘training’ to ‘cloud​admin’ clouduser1 to a) cut the line on space from a file

$ sudo groupmod -n cloudadmin system and print 1st to 4th field $ uniq -d <fi​le.t​xt>

training $ sudo adduser $ echo "my phone number is b) get the count of uniq

cloudu​ser1 887389​3" | cut -d " " -f lines in a file {nl}} $


1-4 b) uniq -c <fi​len​ame​>

Editor​/Text manipu​lation commands change the delimiter space


with column
awk command for pattern scanning & diff, get the
$ echo "​hello world" |
processing: differ​ences by
cut -d " " -f 1-2 --
1. Convert text from upper case to comparing files
lower case line by line outp​ut-​del​imi​ter=%

$ echo "​SAMPLE TEXT" | awk '{print $ diff Sort is to sort file, records, lists tr is to translate or delete
tolowe​r($0)}' file1.txt etc: charac​ters
2 Print the next word after found a file2.txt a) sort file contents of a) translate all lowercase

pattern text file (-r option to letters to upper case in a

e.g. print the next work after reverse sorting) file

‘reach:’ appear in syslog file $ sort file.txt $ cat filename | tr

$ awk b) sort based on column “[:low​er:]” “[:upp​er:]”

'{for(​i=1​;i<​=NF​;i+​+)i​f($​i==​"​rea​ch:​"​)p number b) translate white spaces

rint $(i+1)}' /var/log/syslog $ df -kh | sort -k 5 to tabs

3. Trim the white spaces $ cat filename | tr

echo ' aws <co​mma​nd> help ' | awk [:space:] ‘\t’

'{gsub(/^ +| +$/,"")}1' c) remove all digits from

4. Print the selected columns from string

command output. $ echo "my mob number

E.g. from df command interested in 880392​23" | tr -d

only filesystem and use% column data [:digit:]

$ df -kh |awk '{print $1 " " $5}' d) Just get the digits

5. use regex as a field separator, from string

e.g input field separator as / or = $ echo "my mob number

e.g. 880392​23" | tr -cd

$ awk -F"=​|:" '{print $2}' [:digit:]

input text as
‘dnsco​nf=​/et​c/r​eso​lv.c​onf’ or
‘dnsco​nf:​/et​c/r​eso​lv.c​onf’ for both
same command will work

By rakesh zingade Not published yet. Sponsored by Readability-Score.com


cheatography.com/rakesh- Last updated 2nd December, 2018. Measure your website readability!
zingade/ Page 2 of 4. https://readability-score.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/

Editor​/Text manipu​lation commands (cont) Network related commands (cont)

tee, is a command which reads the standard sed - stream editor, it scp, secure copy from remote host nmap, check open ports
input and write into standard output and also is used for filtering a) copy file from remote host on server, generally used
to a file. This is used to redirect logs or data and transf​orming text (syntax) scp -i <pem file> as network explor​ation
to a file: a) Find and <us​ern​ame​>@<​remote ip>​: tool
a) let we have two log files, replace text <f​ile​pat​h> <local destin​ation a) check open ports
file1.log & file2.log and we need to $ echo 'Unix is dirpat​h> on remote host
append file1.log to file2.log multi-user OS' | e.g. $ scp id_rsa.pem $ nmap
$ cat file1.log | tee -a file2.log sed rakesh​@19​2.1​68.5​6.120:/home​/r 172.21​7.2​7.206
b) redirect the command output to a 's/Uni​x/L​inux/' a​kes​h/d​ata.txt . b) list out all
log file b) delete b) copy local file to remote machines from network
$ du --max-​depth=1 -h | sort -hr particular line host that responds to ping
2>&1 | tee du.log from a file $ scp -i id_rsa.pem data.txt $ nmap -sP
(e.g. 5th line) rakesh​@19​2.1​68.5​6.120:/home​/r 192.16​8.5​6.0/24
$ sed '5d' a​kesh c) scan and print

file.txt ports, os & other


c) delete 5th to details about remote

10th line from a host

file $ sudo nmap -sS -A -


$ sed '5,10d' T4 192.16​8.5​6.150

file.txt lsof, list open files by proces​ses netcat, debug and


(check more a) list open files by specific invest​igate network
details in a user a) start a dummy
separate block) lsof -u <us​ern​ame> listening server on

b) find processes running on port 8080


Network related commands specific port $ netcat -l 8080
$ lsof -i TCP:9090 b) send data over
nslo​okup, Query internet netstat, print the network stats,
domain name server listening ports etc some port to server

a) find the IP from a) print all listening ports $ netcat <remote

fqdn $ netstat -plunt server ip> <po​rt>

$ nslookup google.com b) check if server is listening e.g.

b) check the fqdn on port 8080 or not $ netcat

from ip address $ netstat -plunt | grep 8080 192.16​8.5​6.120 8080

$ nslookup c) list stats of all ports (press EOF CNTR+D at

172.21​7.1​67.174 $ netstat -s end)

d) display pid of listening


ports
$ netstat -pt
e) list network interfaces
$ netstat -i

By rakesh zingade Not published yet. Sponsored by Readability-Score.com


cheatography.com/rakesh- Last updated 2nd December, 2018. Measure your website readability!
zingade/ Page 3 of 4. https://readability-score.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/

Network related commands (cont) sed - stream editor (cont)

curl ifconf​ig.co, get the route, show/=​man​ipulate IP routing table \{i\}: matches exactly i sequences 'i is between 0 to 255'
public ip of the machine a) show current routing table \{i,\} : matches more than or equal to i sequences
$ curl ifconf​ig.co $ route -n rege​x1|​reg​ex2 : matches regular expression 1 or regular expression 2

b) add route to particular [a-z​0-9​A-z]: matches any ASCII chars

network e.g. ======​===​===​===​===​===​===​===​===​=======


Exam​ples
make 10.10.7​6.0/24 accessible via
------​---​---​-------
gw 10.10.76.1
# find and replace any os name with Ubuntu
$ route add -net 10.10.76.0
e.g.
netmask 255.25​5.255.0 gw 1.
10.10.7​6.1 input: osname: centOS7
output: osname: Ubantu
ufw, manage firewall host​name, provides hostname of a
2.
a) check firewall machine
input: winOS: Windows-10
status a) get hostname
output: osname: Ubantu
$ sudo ufw status $ sudo hostname 3.
b) enable​/di​sable input: MacOS:​Mac10
firewall output: osname: Ubantu

$ sudo ufw Solution:


key=echo "​<input string​>" | cut -d "​:" -f 1
enable​/di​sable
echo "​<input string​" | sed -e 's/^$k​ey:​\s.$​/$key: Ubantu/g'
first store the key i.e. left side label
sed - stream editor
^ - start of line
Sed - perform basic transf​orm​ations on an input stream i.e. a file or a \s* - zero or more space charaters
stream input from a pipeline. .* - any zero or multiple charaters
Exam​ple: replace all occurr​ences of TCP to UDP in networ​k.log file $ - end of the line

$ sed 's/TCP​/UDP/' networ​k.log > modifi​ed-​net​wor​k.log ------​---​---​---​---​---​---​---​---​-------


Extract the line containing IP address from a file
Common sed command line options
sed -rn '/([0-​9]{​1,3​}\.)​{3​}[0​-9]​{1,​3}/p' /etc/hosts
-i : edit in place i.e. sed -i 's/TCP​/UDP/' networ​k.log
-n <line number​>p e.g. print on line no 30 from networ​k.log sed -n '30p'
networ​k.log
-e : expression e.g. sed -e 's/TCP​/UDP/' networ​k.log
[ here 's' stand for substitute ]
Basic regular expression overview
. : (dot) matches any single character
* : matches a sequence of zero or more instances e.g.
$ echo 'hostn​ame​=lo​cal​hos​t.m​yor​g.com' | sed
's/l.​l/m​yap​pse​rver/' *
^ : indicates the beginning of the line
$ : indicates the end of the line
[list] or [^li​st] :matches any single char in a list. e.g. [1-9] matches any
digit from 1 to 9
\+ : As *, matches any single or multiple instances of chars
\?: As *, matches any zero or one instances of chars

By rakesh zingade Not published yet. Sponsored by Readability-Score.com


cheatography.com/rakesh- Last updated 2nd December, 2018. Measure your website readability!
zingade/ Page 4 of 4. https://readability-score.com

You might also like