Sometimes you can forget terminal commands in Linux and saving them on your computer or a peace of paper as a cheat sheet is a good practice. This list is not complete but it contains most commonly used commands. Feel free to add your most commonly used commands in the comments below and share the list.
Users
- Id - Details about the user (uid, gid, and group)
- last – Lists information about last logins to the system including time, username, ip address and session length.
- who - View authorized users
- groupadd "testgroup" – Creates a group named “testgroup”
- adduser NewUser - Adds user named “NewUser”
- userdel NewUser - Deletes user named “NewUser”
- usermod NewUser Modifies “NewUser” user’s information
Navigate by Directories
- cd /. - Goes to core directory
- cd - Goes to the home directory ($HOME variable)
- cd /root - Goes to the /root directory
- cd .. – Goes to one level lower
- cd /root/.ssh – Goes to hidden .ssh folder
Working with Files
- ls -al - Lists files and directories
- pwd - Displays the current working directory
- mkdir NewFolder - Creates new directory named “NewFolder”
- rm NewFile - Deletes file named “NewFile”
- rm -f NewFile - Forcibly deletes file named “NewFile”
- rm -r NewFolder - Deletes directory named “NewFolder” recursively
- rm -rf NewFolder – Force deletes a directory named “NewFolder” recursively
- cp oldfile1 newfile2 - Copies contents of oldfile1 to newfile2
- cp -r olddir1 newdir2 - Recursively copies directory “olddir1” to “newdir2”. Dir2 will be created if it does not exist.
- mv oldfile1 newfile2 - Renames “oldfile1” to “newfile2”
- ln -s /etc/log/file logfile - Creates a shortcut to file
- touch newfile - Creates empty file named newfile
- cat > newfile - Places STDIN into newfile
- more newfile - Outputs newfile’s contents in parts
- head newfile - Outputs the first 10 lines of the newfile
- tail newfile - Output the last 10 lines of the newfile
- gpg -c newfile - Encrypts newfile to gpg format with a passphrase and stores it in the same directory
- gpg gpg - Decrypts file
- wc newfile - Prints the number of bytes, words and lines of the newfile
File/Directory Permissions
- chmod 777 /root/ssh – Configures rwx(read, write, execute) permissions to ssh file for everyone who has access to the server (owner, group, others)
- chmod 755 /root/ssh - Configures rwx permissions to the owner and r_x to the group and others
- chmod 766 /root/ssh – Configures rwx permissions for owner and rw for group and others
- chown newuser newfile - Changes newfile’s owner to newuser
- chown newuser:newgroup newfile - Changes owner and group-owner for the newfile to newuser and newgroup
- chown newuser:newgroup newfolder - Changes owner and group-owner for newfolder directory to newuser and newgroup
- stat -c "%U %G" newfile – displays the newfile user and group owners
Search
- grep searchargument newfile - Searches for searchargument in newfile
- grep -r searchargument newfolder - Searches recursively through all files in newfolder for searchargument
- locate newfile - Shows all locations of newfile
- find /etc/ -name "searchargument" - Finds files with a name that starts with searchargument in the /etc directory
- find /etc/ -size +50000k - Find files larger than 50000k in the /etc directory
Archiving
- tar -cf archive.tar newfile - Create 'archive.tar' archive from 'newfile' file
- tar -xf archive.tar - Unpack 'archive.tar' file
- tar -zcvf archive.tar.gz /var/log/ - Create archive from /var/log directory
- gzip newfile - Compress the newfile (it will have a .gz extension)
Install from Packages
- rpm -i pkg_program.rpm - Installs rpm package (CentOS, RHEL...)
- rpm -e pkg_name - Removes rpm package (CentOS, RHEL...)
- dnf install pkg_name - Installs package with dnf from repository. Previously YUM was used, but recently YUM has been replaced by DNF. (CentOS, RHEL...)
- dpkg -i pkg_name - Installs from a deb package (Debian, Ubuntu, Mint...)
- dpkg -r pkg_name - Removes the deb package (Debian, Ubuntu, Mint...)
- apt install pkg_name - Installs package from repository (Debian, Ubuntu, Mint...)
- apt remove pkg_name - Removes package (Debian, Ubuntu, Mint...)
- apt upgrade && apt update - Update packages in your system (Debian, Ubuntu, Mint...) and updates the repositories after that
Processes
- ps - Outputs the currently running processes
- ps aux | grep 'bash' - Find the id of the process 'bash'
- pmap -x 11 - Maps the process with PID = 11 in the process memory
- top – Shows all running processes
- kill pid - Ends process by pid
- killall process - End all processes with the name “process”
- pkill process-name - Send signal to a process
- bg - Send a paused process to background execution
- fg - Bring a running process out of the background
- fg process - Bring process named “process” out of the background
- lsof - Lists of files which processes use
- renice 19 PID - Sets the lowest process priority
- pgrep bash - find bash process ID
- pstree – Shows tree view of the processes
System
- uname - Shows system Information
- uname -r – Shows Linux kernel information
- uptime - How long the system has been running and load average
- hostname – Shows hostname
- hostname -i – Shows IP address of the host
- last reboot – Shows history of reboots
- date – Shows date and time
- timedatectl - Outputs and changes the date and time
- cal - Outputs calendar
- w - Displays the users working in the system
- whoami - Displays your username
- finger root – Shows root user information (needs installation with “apt-get install finger”)
Hardware
- dmesg - Displays the system messages at boot
- cat /proc/cpuinfo - Shows CPU info
- cat /proc/meminfo – Shows RAM info
- lshw – Shows hardware info
- lsblk - Shows Hard drive info
- free -m - Frees memory: RAM and swap (-m switch in MB)
- lspci -tv – Shows information about PCI devices in tree view
- lsusb -tv - Displays USB devices in a tree view.
- dmidecode – Shows BIOS device information
- hdparm -i /dev/xda – Shows disk info
- hdparm -tT /dev/xda – Shows read-write speed of xda
- badblocks -s /dev/xda – Shows test for badblocks
Using the Disk
- df -h - Shows the free space on the mounted partitions (in bytes)
- df -i - Shows free inodes on the filesystem
- fdisk -l - Shows Disk, partition, and filesystem information
- du -sh - Displays unallocated space on mounted partitions in MB, GB, TB
- findmnt - Displays all mount points
- mount /dev/sdb1 /mnt - Mounts partition 1 of disk sdb to /mnt
Network
- ip addr show - Shows IP addresses of all available network interfaces
- ip address add 192.168.0.1/24 dev eth0 - Assigns the address 192.168.0.1 to eth0
- ifconfig - Shows IP addresses of all available network interfaces
- ping 168.0.1 - Sends an ICMP echo request to connect to the host under 192.168.0.1
- whois domain – Shows information about the domain name
- dig domain - Gets DNS information about a domain
- dig -x 192.168.0.1 - Inverses name resolution
- host us – Shows host address resolution
- hostname -I – Shows local addresses
- wget file_name – Downloads a file
- netstat -pnltu - Shows all the ports being listened to on the host(needs “apt-get install net-tools”)
Remote connection
- ssh root@host - Connects to a remote host via ssh as root
- ssh -p port_number user@host - Connects to remote host if ssh port other than 22 is used
- ssh host – Uses default connection as the current user
- telnet host – Uses telnet connection (port 23)