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 (cont)
To know the OS type: To know
To create
the CPU
a new
archit
group:
ecture:
$ uname -o $ uname
$ sudo
-m groupadd <group name>
To check the kernel version: To gete.g.
the OS$ name,
sudo release,
groupadd training
version:
$ uname -r To /etc/os-release
$ cat delete the existing group:
To list the system hardware: $ the
To get sudo
CPUgroupdel
details: <group name>
$ lshw e.g.
$ lscpu $ sudo groupdel training
To check system memory: To check the virtual memory stats:
$ free -h $ vmstat -S m
Free memory cache, dentries and inode (with root): $ echo 3 > To print the process specific memory utilizations:
/proc/sys/vm/drop_caches $ ps aux --sort=-%mem
To search packages for installation: To installed package:
$ apt search <package name> $ sudo apt-get install <package name>
e.g.:
$ apt search python-boto
Print the groups to which the current user is associated:
To uninstall package: To list$ the mounted disk drives:
groups
$ sudo apt-get remove <package name $ df -kh
To mount the volume: To list biggest files from directory (biggest 5):
(create the directory first to mount volume) $ sudo du -a /dir/ | sort -n -r | head -n 5
Change the group name:
$ mkdir -p <directory path e..g /mount-vol>
$ sudo groupmod –n <new group name> <old group name>
$ sudo mount <src path> <above created dir path>
e.g. I want to change the groupname ‘training’ to ‘clouda
Find the file (search for a file): Search the text string in a directory and print filename containing that
$ find <dir path> -name <filename> -print string: $ sudo groupmod -n cloudadmin training
e.g. to find app.log in /var directory $ file /var -type f -print | xargs grep <search text>
$ find /var -name app.log –print`
File the text string from a given directory:
$ grep -rIn <search text> <directory path>
User admin Commands
To know the group/user exists on the Check user added or not
system: into system:
$ getent group <group name> $ id <username>
$ getent passwd <user name> e.g. $ id clouduser1
By rakesh zingade Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/rakesh- Last updated 2nd December, 2018. Learn to solve cryptic crosswords!
zingade/ Page 1 of 4. http://crosswordcheats.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/
Editor/Text manipulation commands Editor/Text manipulation commands (cont)
awk command for pattern scanning & processing: cut, cutting out the sections from lines: diff, get the
1. Convert text from upper case to lower case $ cut -d “delimiter” -f <field> <file.txt> files line by
$ echo "SAMPLE TEXT" | awk '{print tolower($0)}' a) cut the line on space and print 1st to 4th field$ diff
2 Print the next word after found a pattern $ echo "my phone number is 8873893" | cut -d " " -f 1-4 b
e.g. print the next work after ‘reach:’ appear in syslogchange
file the delimiter space with column
$ awk '{for(i=1;i<=NF;i++)if($i=="reach:")print
$ echo
$(i+1)}'
"hello/var/log/syslog
world" | cut -d " " -f 1-2 --output-deli
3. Trim the white spaces %
echo ' aws <command> help ' | awk '{gsub(/^ +| +$/,"")}1'
Sort is to sort file, records, lists etc:
4. Print the selected columns from command output. a) sort file contents of text file (-r option to reverse so
E.g. from df command interested in only filesystem and use% column
$ sort data
file.txt
$ df -kh |awk '{print $1 " " $5}' b) sort based on column number
5. use regex as a field separator, $ df -kh | sort -k 5
e.g input field separator as / or = e.g.
$ awk -F"=|:" '{print $2}'
input text as ‘dnsconf=/etc/resolv.conf’ or ‘dnsconf:/etc/resolv.conf’ for both same comma
nd will work
By rakesh zingade Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/rakesh- Last updated 2nd December, 2018. Learn to solve cryptic crosswords!
zingade/ Page 2 of 4. http://crosswordcheats.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/
Editor/Text manipulation commands (cont) Network related commands (cont)
tee, is a command which reads the standard input and write into standard output
scp, secure
and also
copy
to a
from
file.remote
This is host
used to redirect logs or data sed - stream edit
to a file: a) copy file from remote host a) Find and r
a) let we have two log files, file1.log & file2.log and we need toscp
(syntax) append file1.log
-i <pem to file2.log
file> <username>@<remote
$ echoip>:<f
'Unix
$ cat file1.log | tee -a file2.log irpath>
b) redirect the command output to a log file e.g. $ scp id_rsa.pem rakesh@192.168.56.120:/home/ra
b) delete pa
$ du --max-depth=1 -h | sort -hr 2>&1 | tee du.log b) copy local file to remote host $ sed '5d' f
$ scp -i id_rsa.pem data.txt rakesh@192.168.56.120
c) delete 5t
lsof, list open files by processes $ sed '5,10d
a) list open files by specific user (check more
lsof -u <username>
Network related commands b) find processes running on specific port
nslookup, Query internet domain name server netstat, print the network stats,
$ lsof listening ports etc
-i TCP:9090
a) find the IP from fqdn a) print all listening ports
$ nslookup google.com $ netstat -plunt
b) check the fqdn from ip address b) check if server
curlis listening
ifconf onpublic
ig.co, get the portip8080
of the or not
machine
$ nslookup 172.217.167.174 $ netstat -plunt $
| curl
grep ifconfig.co
8080
c) list stats of all ports
$ netstat -s
d) display pid of listening ports
$ netstat -pt
e) list network interfaces
$ netstat -i
ufw, manage firewall
a) check firewall status
$ sudo ufw status
b) enable/disable firewall
$ sudo ufw enable/disable
By rakesh zingade Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/rakesh- Last updated 2nd December, 2018. Learn to solve cryptic crosswords!
zingade/ Page 3 of 4. http://crosswordcheats.com
Linux Commands to know by DevOps Engineer Cheat Sheet
by rakesh zingade via cheatography.com/71377/cs/18096/
sed - stream editor sed - stream editor (cont)
Sed - perform basic transformations on an input stream i.e. a file or a stream
$ -input
end from
of thealine
pipeline. -------------------------------------
Example: replace all occurrences of TCP to UDP in network.log file Extract the line containing IP address from a file
sed -rn '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' /etc/hosts
$ sed 's/TCP/UDP/' network.log > modified-network.log
Common sed command line options
-i : edit in place i.e. sed -i 's/TCP/UDP/' network.log
-n <line number>p e.g. print on line no 30 from network.log sed -n '30p' network.log
-e : expression e.g. sed -e 's/TCP/UDP/' network.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 'hostname=localhost.myorg.com' | sed 's/l.l/myappse
rver/' *
^ : indicates the beginning of the line
$ : indicates the end of the line
[list] or [^list] :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
\{i\}: matches exactly i sequences 'i is between 0 to 255'
\{i,\} : matches more than or equal to i sequences
regex1|regex2 : matches regular expression 1 or regular expression 2
[a-z0-9A-z]: matches any ASCII chars
=====================================
Examples
-------------------
# find and replace any os name with Ubuntu
e.g.
1.
input: osname: centOS7
output: osname: Ubantu
2.
input: winOS: Windows-10
output: osname: Ubantu
3.
input: MacOS:Mac10
output: osname: Ubantu
Solution:
key=echo "<input string>" | cut -d ":" -f 1
echo "<input string" | sed -e 's/^$key:\s.$/$key: Ubantu/g'
first store the key i.e. left side label
^ - start of line
\s* - zero or more space charaters
.* - any zero or multiple charaters
By rakesh zingade Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/rakesh- Last updated 2nd December, 2018. Learn to solve cryptic crosswords!
zingade/ Page 4 of 4. http://crosswordcheats.com