Display free and used memory (-h for human readable, -m for MB, -g for GB.)
# free -h
Display DMI/SMBIOS (hardware info) from the BIOS
# dmidecode
PERFORMANCE MONITORING AND STATISTICS
Display and manage the top processes
# top
Display processor related statistics
# mpstat 1
Display virtual memory statistics
# vmstat 1
Display the last 100 syslog messages (Use /var/log/syslog for Debian based systems.)
# tail 100 /var/log/messages
Capture and display all packets on interface eth0
# tcpdump -i eth0
Monitor all traffic on port 80 ( HTTP )
# tcpdump -i eth0 port 80
Execute "df -h", showing periodic updates
# watch df –h
USER INFORMATION AND MANAGEMENT
Display the user and group ids of your current user.
# id
Display the last users who have logged onto the system.
# last
Show who is logged in and what they are doing (w or who).
# w
Create a group named "test"
# groupadd test
Create an account named john, with a comment of "John Smith" and create the user's home directory
# useradd -c "John Smith" -m john
Delete the john account
# userdel john
Add the john account to the sales group
# usermod -aG sales john
FILE AND DIRECTORY COMMANDS
List all files in a long listing (detailed) format
# ls –al
Display the present working directory
# pwd
Create a directory
# mkdir directory
Remove (delete) file
# rm file
Remove the directory and its contents recursively
# rm -r directory
Force removal of file without prompting for confirmation
# rm -f file
Forcefully remove directory recursively
# rm -rf directory
Copy file1 to file2
# cp file1 file2
Copy source_directory recursively to destination. If destination exists, copy source_directory into destination, otherwise create destination with the contents of source_directory
# cp -r source_directory destination
Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
# mv file1 file2
Create symbolic link to linkname
# ln -s /path/to/file linkname
Create an empty file or update the access and modification times of file
# touch file
View the contents of file
# cat file
Browse through a text file
# less file
Display the first 10 lines of file
# head file
Display the last 10 lines of file
# tail file
Display the last 10 lines of file and "follow" the file as it grows
# tail –f file
PROCESS MANAGEMENT
Display your currently running processes
# ps
Display all the currently running processes on the system
# ps -ef
Display and manage the top processes
# top
Kill process with process ID of pid
# kill <PID>
Kill all processes named processname
# kill <process name>
Start program/script in the background
# <program name>&
Display stopped or background jobs
# bg
Brings the most recent background job to foreground
# fg
Brings job n to the foreground
# fg n
FILE PERMISSIONS
Linux chmod example
PERMISSION
rwx rwx rwx
# chmod 777 filename
rwx rwx r-x
# chmod 775 filename
rwx r-x r-x
# chmod 755 filename
rw- rw- r--
# chmod 664 filename
rw- r-- r--
# chmod 644 filename
LEGEND
u= User
g= Group
o= Others
r = Read
w = write
x = execute
s = setuid
- = no access
NETWORKING
Display all network interfaces and ip address
# ifconfig -a
Display eth0 address and details
# ifconfig <eth0>
Query or control network driver and hardware settings
# ethtool <eth0>
Send ICMP echo request to host
# ping <host-ip>
Display whois information for domain
# whois <domain>
Display DNS information for domain
# dig <domain>
Reverse lookup of IP_ADDRESS
# dig -x <IP_ADDRESS>
Display DNS ip address for domain
# host <domain>
Display the network address of the host name
# hostname -i
Download http://domain.com/file
# wget http://domain.com/file
Display listening tcp and udp ports and corresponding programs
# netstat -nutlp
EDITING THE FILES
To enter vi, nano and editi file
# vi filename <Return>
- To enter insert mode, use: i key or Ins Key.
- Type in the text.
- To leave insert mode and return to command mode, press: <Esc>
- In command mode, save changes and exit vi by typing: :wq <Return> You are back at the Unix prompt.
- To exit without saving changes, use :q. To override changes and exit without saving, use :q!
# nano filename
DIRECTORY NAVIGATION
To go up one level of the directory tree
(Change into the parent directory.)
# cd ..
Go to the $HOME directory
# cd ~/
Change to the /etc directory
# cd /etc
DISK USAGE
Show free and used space on mounted filesystems
# df -h
Show free and used inodes on mounted filesystems
# df -i
Display disks partitions sizes and types
# fdisk -l
Display disk usage for all files and directories in human readable format
# du -ah
Display total disk usage off the current directory
# du -sh
FILE TRANSFERS
Secure copy file.txt to the /tmp folder on server
# scp file.txt server:/tmp
Copy *.html files from server to the local /tmp folder.
# scp server:/var/www/*.html /tmp
Copy all files and directories recursively from server to the current system's /tmp folder.
# scp -r server:/var/www /tmp
Synchronize /home to /backups/home
# rsync -a /home /backups/
Synchronize files/directories between the local and remote system with
# rsync -avz /home server:/backups/
SSH LOGINS
Connect to host as your local username
# ssh host
Connect to host as user
# ssh user@host
Connect to host using port
# ssh -p port user@host
SEARCH
Search for pattern in file
# grep pattern file
Search recursively for pattern in directory
# grep -r pattern directory
Find files and directories by name
# locate name
Find files in /home/john that start with "prefix"
# find /home/john -name 'prefix*'
Find files larger than 100MB in /home
# find /home -size +100M
INSTALLING PACKAGES
(RPM based distributions)
Search for a package by keyword
# yum search keyword
Install package
# yum install package
Display description and summary information about package
# yum info package_name
Install package from local file named package.rpm
# rpm -i package.rpm
Remove/uninstall package
# yum remove package
Install software from source code
# tar zxvf sourcecode.tar.gz
# cd sourcecode
#./configure
# make
# make install
(DPM based distributions)
Search for a package by keyword
# apt-cache search keyword
Install package
# apt install package_name
Display description and summary information about package
# apt show package_name
Remove/uninstall package
# apt remove --purge package_name
Install software from source code
# dpkg -i <package_name.deb>
ARCHIVES (TAR FILES)
Create tar named archive.tar containing directory
# tar cf archive.tar directory
Extract the contents from archive.tar
# tar xf archive.tar
Create a gzip compressed tar file name archive.tar.gz