My Linux commands cheatsheet

The following Linux commands compilation are my personal Linux commands that I find useful in my daily work:

Display running tasks:
top

or

htop
Display current process running:
ps ax

or

ps -ef
Backup a mysq database:
mysqldump -u [uname] -p [dbname] > [backupfile.sql]
Restore a mysq database from backup file:
mysql -u [uname] -p [dbname] 
List all storage partition:
fdisk -l
Display disk space usage:
df -h
Search or find big files Linux (50MB) in current directory:
find . -type f -size +50000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Change file ownership recusively:
chown -R apache:apache /var/htdocs
Create a directory with no error if existing and auto create parent directories as needed:
mkdir -p newfolder/innerfolder/
Copy files recursively:
cp -rf /var/htdocs /home/roland/public_html
Delete files recusively:
rm -rf /var/htdocs
Copy file from local computer to remote computer (scp ):
scp backup.tar.gz root@192.168.1.88:/home/roland
Copy file from remote computer to local computer (scp ):
scp root@192.168.1.88:/home/roland/backup.tar.gz /download/
Compressing using tar:
tar zcvf backup.tar.gz /backup/*
Extracting tar:
tar zxvf backup.tar.gz
Extracting gzip:
gunzip backup.gz
Download a file from remote site:
wget http://ftp.drupal.org/files/projects/drupal-8.x-dev.tar.gz

or

curl -O http://ftp.drupal.org/files/projects/drupal-8.x-dev.tar.gz
Check and install a software:

which java
yum list available java*
yum install java-1.8.0-openjdk.x86_64
which java
java -version
Display content of a text file:
cat README.txt
Edit a file with VIM:
vi index.php
Delete all hidden svn folders:
find . -name *svn -print0 | xargs -0 rm -rf

or

rm -rf `find . -type d -name .svn`
Compare files including sub folders:
diff -bwBqr "!tinymce" htdocs\sites\all\libraries\tinymce > diff.txt
Compare and auto patch:
diff -bwBu old.txt new.txt | patch old.txt
Network commands:

ifconfig
ifup eth0
ifdown eth0
netconfig
Soft linking a file:
ln -s ttyCL0 ttyS0
Test if port is open/listening

netstat -tuplen
nc 127.0.0.1 8080 

or


nmap -p 8080 127.0.0.1
Health check of a webpage:

curl -Sf www.webfoobar.com/node/1 > /dev/null
Health check of a webpage bypassing CDN:

curl -H 'Host: www.webfoobar.com' -s -D - -o /dev/null 'http://xxx.xxx.xxx.xxx/node/1'
Add port (to specific line number, eg. 5) to unblock it from firewall and backup the rules:

iptables -I INPUT 5 -p tcp -m tcp --dport 8080 -j ACCEPT
iptables --line -vnL
service iptables save
service iptables restart
iptables-save > /home/roland/conf.bak/iptables.bak
Restore iptables rules from backup file:

iptables-restore < /home/roland/conf.bak/iptables.bak
Add a rule to iptables:

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
iptables --line -vnL
service iptables save
service iptables restart
Remove a rule from iptables:

iptables -D INPUT -p tcp --dport 8080 -j ACCEPT
iptables --line -vnL
service iptables save
service iptables restart
Shows successful login attempts:

last
Shows bad login attempts:

lastb

or:


cat /var/log/secure | grep 'sshd.*Invalid'
Watch last 20 log of failed attempts refreshing every one second:

watch -n 1 tail -n 20 /var/log/secure
Block an IP:

iptables -A INPUT -s XXX.XXX.XXX.XXX -j DROP
service iptables save
service iptables restart

or edit /etc/hosts.deny to append:


ALL: XXX.XXX.XXX.XXX
Unblock an IP:

iptables -D INPUT -s XXX.XXX.XXX.XXX -j DROP
service iptables save
service iptables restart
Benchmark your site (Anonymous users):

ab -n 500 -c 10 -r [domain]
ab -n 500 -c 10 -r http://www.webfoobar.com/
Benchmark your site (Authenticated users):

ab -c 1 -n 100 -C [session name]=[session value] [domain]
ab -c 1 -n 100 -C SESS35ff2c61bdd320e6efaf60591161691a=OteOH_VuieYK9_Y-culzTEsQJevDfIjkUC50c-aO1BA http://www.webfoobar.com/
Log network traffic:
tcpdump -s 0 -i eth0 -w wireshark.pcap
List top IP network traffic:
netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1
Monitor server resources (every 3 seconds refresh rate):
vmstat 3
Know the IP table in the network:
arp -a
Change user password:
passwd
Compile source code of a software:

autoreconf -i
./configure
make
make install
Hard Disk Clone:
dd if=/dev/sda of=/dev/sdb
Making a Hard Disk Image File:
dd if=/dev/sda of=sda.img
Restoring from an Image File:
dd if=sda.img of=/dev/sda
Logs out from SSH that prevents saving the current session's executed commands to bash history:
kill -9 $$
Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.