Linux Cheat Sheet

A Cheat Sheet is a concise reference that provides guidance for executing specific tasks/commands.

Below you will find a Linux Cheat Sheet I prepared at the end of a training (also available in PDF format). If you’re a Linux Administrator it’s always useful to create your own cheat sheet.

Basic commands
cd <directory> Change to <directory>
ls, ls -F, ls -l List directory content
mkdir Create new directory
pwd Print current directory
mv Move/rename files/folders
rm Delete file
rm -Rf <dir> Delete <dir> and everything under
more, less, cat Look at files content
command > file Overwrite file with output of command
command >> file Append output of command to file
cmd1 | cmd2 Use output of command 1 as input of command 2
command < file Use file as input of command
chmod Change permissions
chown Change ownership
ln -s fileName linkName Create a symbolic link to a file
Process management
ps, ps -aux, ps -ef Display running processes
top Display all processes
Ctrl-C Stop current process
jobs Show launched processes
Ctrl-Z Suspend active process
bg Send process to background
fg Send process to foreground
bg %1 Send process number 1 to background
fg %2 Send process number 2 to foreground
kill Kill a process
kill -9 Force process to be killed
Archiving & compressing files
tar cvf bak.tar /etc /home Put /etc and /home in bak.tar
tar xvf bak.tar Extract bak.tar content
gzip bak.tar Compress bak.tar
gzip -9 bak.tar -9: use highest compression level
gunzip bak.tar.gz Decompress bak.tar.gz
Finding text in files
grep hello *.txt Find lines containing “hello” in .txt files
grep -v hello *.txt Find all lines except ones containing “hello”
grep -i HeLLo *.txt Ignore case
grep -R hello /home/* Find lines containing “hello” in all files under /home
grep -lR hello /home/* List only name of files containing “hello”
grep ‘[Ss]mug’ files Search for ‘Smug’ or ‘smug’ using regular expression
Email utilities
mail user@domain Send email to user
mutt Powerful text based email client
Managing users
adduser, useradd Add a new user
deluser, userdel Delete a user
passwd Change user password
Various network tools
nslookup, dig Query DNS servers
ping Test if server is alive
traceroute w3.w3.com Display route trace to w3.w3.com
nmap 10.0.0.2 Show open ports for 10.0.0.2
Monitoring network traffic
netstat -an Shows all connections to the server
netstat -r Show routing table
netstat -s Display network statistics
ethereal Packet sniffer
User information & communication
who, users List of users logged into a machine
lastlog Most recent login of all users
write user1 Send a message to user1
Monitoring utilities
top Dynamic realtime view of a running system
iostat -dkx 2 I/O statistics for devices, partitions & NFS
vmstat 2 Processes, memory, swap, I/O, traps, cpu
dstat Generates system resources statistics
ps -aux / ps -ef Processes information
Some networking commands
ifconfig Display IP settings
ifconfig eth0 down Disable interface eth0
ifconfig eth0 up Enable interface eth0
ifconfig eth0:0 192.168.2.2 netmask 255.255.255.0 Configure subinterface eth0:0
route -n Display current routing table
route add default gw 10.0.0.33 Add default gateway
route del default gw 10.0.0.33 Delete default gateway
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 Send datagrams to 192.168.2.1 gateway if their destination IP belongs to the 192.168.2.0 network
route add -host 192.168.2.33 gw 192.168.2.12 If the destination address is 192.168.2.33 send the IP datagram to 192.168.2.12 gateway
ipcalc -n 192.11.11.22/27 Compute network address for 192.11.11.22/27
ipcalc -b 192.11.11.22/27 Compute broadcast address for 192.11.11.22/27
Searching for files
find /home -name “*.txt” 2>/dev/null Find all “.txt” files under /home
find /home -name “*.txt” -exec wc -l {} \; 2>/dev/null Find all “.txt” files under /home and execute wc -l on each file found
locate maps Find all files with containing the string “maps”
updatedb Update locate search DB, must be executed before locate in case file(s) has been moved/deleted/added
Package management – rpm & yum for Redhat / dpkg & apt-get for Debian
rpm -i package / dpkg -i package Installing package
rpm -e package / dpkg -r package Erasing package
rpm -qa / dpkg -l List all installed packages
yum install package / apt-get install package Installing package with all dependencies
yum remove package /apt-get remove package Removing package with all dependencies
yum search <string> / apt-cache search <string> Search for package names containing <string>
SSH – Remote login
ssh username@host Login to host with username
SSH Tunneling: Access email securely on mail.server.com
ssh -N -f user@mail.server.com -L 2110/localhost/110 Forward traffic from port 2110 on the local machine to port 110 on the remote machine
ssh -N -f user@mail.server.com -L 2025/localhost/25 Forward traffic from port 2025 on the local machine to port 25 on the remote machine
Pop setup : server = localhost, port = 2110
Smtp setup: server = localhost, port = 2025
Use this setup with your local email client in order to use encrypted ssh tunnel to send/receive email using mail.server.com
IN ONE COMMAND: ssh -N -f user@mail.server.com -L 2110/localhost/110 -L 2025/localhost/25
SSH automatic login with private/public keys
Under machine A execute: “ssh-keygen -t rsa”, leave password blank creates a private key ”id_rsa” and a public key ”id_rsa.pub”
scp $HOME/.ssh/id-rsa.pub user@remotemachine:/tmp/ Move id_rsa.pub to remote machine B
ssh user@remotemachinecat /tmp/id_rsa.pub >> $HOME/.ssh/authorized_keys Connect to remote machine B and append id_rsa.pub to $HOME/.ssh/authorized_keys, “.ssh” permissions are at most 700, “authorized_keys” permissions are at most 600
Now you can connect to machine B without any password from machine A, try: ssh user@remotemachine
Surviving ssh disconnections
nohup <command> & Command will continue executing even if ssh connection is closed
screen Useful tool to create multiple “sub-sessions” which survives disconnections
Remote file transfer
scp Copy using ssl
scp file1 user@remotehost:/tmp/ Copy file1 under /tmp on remotehost
scp user@remotehost:/tmp/hello.txt /home/myuser Copy /tmp/hello.txt from remotehost to /home/myuser
scp -r /home/myuser/* user@remotehost:/back/ Copy recursively everything under /home/myuser/ to /back/ on remote host
rsync Delta-transfer algorithm, transfer only the differences
rsync -avz host:/home/bar /data/tmp Transfer /home/bar directory from host to /data/tmp/bar
rsync -avz host:/home/bar/ /data/tmp Transfer /home/bar files and dirs on host under /data/tmp
rsync -ave ssh /data/tmp/tempo user@machine:/tmp Use ssh to do the rsync, no need for an rsync daemon on server
rsync –delete -ave ssh /data/tmp/tempo user@machine:/tmp Removes deleted file on destination dir ectory, default is to keep files on destination directory even if deleted on source directory
AWK
awk -F: ‘{ print $1 ” ” $3 }’ /etc/passwd Prints user and userid from /etc/passwd
awk -F: ‘ BEGIN {users=0; grp=0}{ users++; if ($4 > 1000) grp++ } END { print “total users: ” users “, users in groups greater than 1000 : ” grp }’ /etc/passwd Computes number of total users and number of users in groups greater than 1000
ls /tmp | awk ‘{print “mv /tmp/”$1” /tmp/NEW”$1}’ | /bin/bash Append the string “NEW” to every file or folder under /tmp
Changing file content
sed “s/pattern1/pattern2/” inputfile > outputfile Replaces pattern1 with pattern2 once per line, result is saved in outputfile
sed “s/pattern1/pattern2/g” inputfile > outputfile Replaces pattern1 with pattern2 for every pattern1 per line
sed -i “s/pattern1/pattern2/” file Changes are made directly in file
sed “/pattern1/d” inputfile > outputfile Deletes lines containing pattern1
sed “y/string1/string2/” inputfile > outputfile Replaces characters in string1 with those in string2
Various
./configure –help | less Check compiling options
./configure && make && make install Compiling from source
which command Finds command location
strings file1 Print the strings of printable characters in file1
lynx http://www.elementn.com Command line browser like Firefox !
wget http://www.example.com/ Downloads home page of www.example.com
wget -r -l 0 http://www.example.com/ Downloads the hole website, if there is no restrictions !
wget -r -A.pdf –no-parent http://url-to-webpage-with-pdfs/ Downloads all pdf files from a certain site
wget –post-data ‘example_name=testing’ www.url.com Send a post request to some site
curl –data-urlencode “name=I am Daniel” www.example.com Send a post request to example.com and perform url encoding
curl -u name:password www.secrets.com HTTP Authentication using name / password
sudo command execute command as root or another user
Prepared by Najib Abi Fadel

6 thoughts on “Linux Cheat Sheet

  1. Robertsor

    I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the net will be much more useful than ever before.

  2. bottes ugg erin

    It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!

Leave a Reply

Your email address will not be published.