Show Menu
Cheatography

linux Cheat Sheet (DRAFT) by

grouping all linux based commands

This is a draft cheat sheet. It is a work in progress and is not finished yet.

List files

listed view
ls -l
show hidden files also
ls -la
show size in human readable
ls -lah
showfile created today
ls -ltr | grep "­$(date '+%b %e')"
show files in tree structure
tree [/path­/to­/fo­lder]

vi editor

quit
:q
force quit
:q!
save and quit
:wq
find foo and replace all with bar
%s/foo­/bar/g

install loadba­lancer

install lb
yum install ipvsadm
enable ip forwarding
echo 'net.i­pv4.ip­_fo­rward = 1' | sudo tee -a /etc/s­ysc­tl.conf 
reload
sudo sysctl -p
check ip forwarding enabled
sysctl net.ip­v4.i­p_­forward
create config file
sudo touch /etc/s­ysc­onf­ig/­ipvsadm
start service
sudo systemctl enable --now ipvsadm
clear iptable rules
sudo ipvsadm -C
add a virtual service
ipvsadm -A -t [Servi­ceI­P:Port] -s [Distr­ibution method]

ansible

export inventory
export ANSIBL­E_I­NVE­NTO­RY=­/ho­me/­use­r1/­inv­entory/
run adhoc command in all nodes
ansible all -a 'uname -r'
run command with multiple forks (defau­lt:5)
ansible all -a 'uname -r' -f 15
run command as different user
ansible all -a uptime -u userx
copy file
ansible all -m copy -a "­src­=te­stfile dest=/­tmp­/te­stf­ile­"
check if yum package is present
ansible all -m yum -a "­nam­e=d­ocker state=­pre­sen­t"
check if yum package is absent
ansible all -m yum -a "­nam­e=i­perf3 state=­abs­ent­"
check if installed yum pacakage is latest
ansible all -m yum -a "­nam­e=d­ocker state=­lat­est­"
sudo pass in ansibl­e-p­laybook
--extr­a-vars "­ans­ibl­e_s­udo­_pa­ss=­abc­d"

encode

encode
echo 'passw­ord­@123' |base64
encode without newline
echo 'passw­ord­@123' |base64 | tr -d \\n
decode
echo 'cGFzc­3dv­cmR­AMT­IzCg==' |base64 -d

curl

ignore certif­icate error
curl -vk https:­//l­oca­lho­st:8080
to grep pattern
curl -v --silent https:­//k­id2­com­p02­7.e­ric­sso­n.s­e:8443 --stderr - | grep [pattern]
curl tcp

system stats

get RHEL version
cat /etc/r­edh­at-­release
get kernel version
uname -r
get cpu count
cat /proc/­cpuinfo | grep processor | wc -l
get processor model
cat /proc/­cpuinfo | grep 'model name' | uniq
get memory
cat /proc/­meminfo |grep MemTotal
get ip addresses
ip r
get all ip details
ip a
get list of logged in users
users
get free disk space
df -h
get disk usage details
du -sh [path]
get id of current user
id
get id of other user
id userx

TCP dump

get tcp dump
tcpdump -i bond0.60 dst 137.10.10.10 and dst port 30000

extend partition

RHEL 6
check volume group
vgdisplay vg_data
volume group extend (if no free space)
vgextend /dev/V­olG­roup00 /dev/sda3
extend logical volume
lvextend -L +2G /dev/m­app­er/­vg_­dat­a-l­v_d­ocker
resize
resize2fs /dev/m­app­er/­vg_­dat­a-l­v_d­ocker
RHEL 7
 
xfs_growfs /dev/m­app­er/­vg_­dat­a-l­v_d­ocker

iperf3

install iperf3
yum install iperf3
run iperf server on default port 5201
iperf3 -s -f K  
(k, m, g for Kbits, Mbits, Gbits or K, M, G for KBytes, Mbytes, Gbytes)
run iperf server on specific port
iperf3 -s -p 3000
run iperf as daemon
iperf3 -s -D > iperf3log
run iperf client
iperf3 -c 192.16­8.10.1 -f K
run 2 parallel session
iperf3 -c aedup1mst1 -f G -P 2
run test in reverse direction
iperf3 -c aedup1mst1 -f G -R
run test in bi direction
iperf3 -c aedup1mst1 -f G -d
get server output in client
iperf3 -c aedup1mst1 -f G --get-­ser­ver­-output
set windos socket­/bu­ffe­rsize
iperf3 -c 192.16­8.10.1 -f K -w 500K
iPerf3 is a tool for active measur­ements of the maximum achievable bandwidth on IP networks. It supports tuning of various parameters related to timing, buffers and protocols (TCP, UDP, SCTP with IPv4 and IPv6). For each test it reports the bandwidth, loss, and other parame­ters.

find

find file matching pattern
find / -name pattern
find files based on content
grep -ir "­pat­ter­n" *

Generic

Cancel
ctrl + c
Exit
ctrl + d
Clear screen
clear
Clear screen
ctrl + l
 

zip

zip a folder
zip -r [outpu­t.zip] [folder name]
unzip a folder
unzip [filename]

tar

tar a folder
tar -cvf [outpu­t.tar] [/dirname]
untar a file to diff folder
tar -C [/myfo­lder] -xvf [yourf­ile.tar]
untar to current folder
tar -xvf [yourf­ile.tar]
untar a .tar.gz file
tar -zxvf [yourf­ile.ta­r.gz]

change host name

#method 1
edit file hostname
echo "­gee­kla­b" > /etc/h­ostname
reboot server
sudo reboot
check hostname
hostname
#method 2
check current hostname
hostna­mectl status
update hostname
hostna­mectl set-ho­stname geeklab
or
hostna­mectl set-ho­stname "­Geeks LAB"
relogin and verify
hostna­mectl

session timeout

open file
sudo vi /etc/s­sh/­ssh­d_c­onfig
edit params
Client­Ali­veI­nterval
and
Client­Ali­veC­ountMax
reload sssh
sudo systemctl reload sshd
Timeout value = Client­Ali­veI­nterval * Client­Ali­veC­ountMax

password less login setup

create key in host machine
ssh-keygen -t rsa
Copy key to target
ssh-co­py-id [target]
copy content from .ssh/i­d_r­sa.pub to .ssh/a­uth­ori­zed­_keys

stop kernel messages

open file
sudo cat /etc/s­ssd­/ss­sd.conf
add in last line
ad_gpo­_ig­nor­e_u­nre­adable = True
restart sssd
sudo systemctl restart sssd
reboot if still persists
sudo reboot

output formatting

capture pattern in output
| grep [pattern]
capture x lines after pattern
| grep [pattern] -A [x]
capture x lines before pattern
| grep [pattern] -B [x]
print only column x
| awk '{print $[x]}'
count output lines
| wc -l
grep curl output
curl -v --silent https:­//g­oog­le.c­om:443 --stderr - | grep [pattern]

edit gateway

#temp
delete existing gateway
sudo route delete default gw 10.10.1­0.10 bond0.200
add new gateway
sudo route add default gw 137.10.1­0.10 bond0.60
#permanent
edit file
sudo vi /etc/s­ysc­onf­ig/­net­wor­k-s­cri­pts­/if­cfg­-bo­nd0.200
restart network
sudo /etc/i­nit.d/­network restart

kernel upgrade

go to yum folder
cd /etc/y­um.r­epos.d
create a bkup folder
mkdir bkp1
move all repos to it
'mv x.repo bkp1'
enable rhel 7 rpms
subscr­ipt­ion­-ma­nager repos --enable rhel-7­-se­rve­r-rpms
list available kernel
yum list kernel
install required kernel
yum install kernel 3.10.0­-11­27.1­8.2.el7
reboot
reboot

create partition

create physical volume
sudo pvcreate /dev/n­vme0n1
create volume group
sudo vgcreate vg_data /dev/n­vme0n1
list volume group and check
sudo vgs
create logical volume
sudo lvcreate -n lv_docker -L +100G vg_data
list logical volume and check
sudo lvs
create a directory to map (if not exists)
sudo mkdir /var/l­ib/­docker
check file system packages
sudo ls -l /usr/s­bin­/mkfs.*
 
sudo mkfs -t xfs /dev/m­app­er/­vg_­dat­a-l­v_d­ocker
add entry in /etc/fstab
sudo sed -i '$a /dev/m­app­er/­vg_­dat­a-l­v_d­ocker /var/l­ib/etcd xfs defaults 0 0' /etc/fstab
mount
sudo mount -a
check if mounted
sudo df -h

delete partition

list volume group
sudo vgs
remove volume group
sudo vgremove vg_data
start disk utility
sudo fdisk /dev/n­vme0n1
list partition
p
delete partition
d
save and exit
w
remove entries from /etc/fstab and unmount first.