Linux Cheat Sheet
While I'm still new to Linux I've found the following commands and applications really useful to further my understanding.
Here's a list of commands I use day to day, mostly in Ubuntu. Please be aware that some of them may be different based on the distribution you use. If you're completely new to Linux I'd recommend getting Ubuntu up and running which will allow you to test these commands. Once you're familiar you'll begin to understand the alternatives to them in different distributions.
How to use this guide
Please note that the text between <
and >
in the examples below (i.e. <COMMAND>
) should not be interpreted literally and should be replaced with a value relevant to what you're trying to do. e.g. If it says <DIRECTORY>
replace it with the directory you are trying to perform an action on.
Tips
Help
<COMMAND> --help
You can usually type after any command and you'll get some information about how to use it, what arguments it accepts and what order they need to be in. If you're ever stuck on a command this will help to shed some light on the situation.
Important: Don't forget to remove any arguments you've added to the command before adding the help flag. For instance if I want help on the cd
command I wouldn't type cd /home --help
because /home
is an argument for cd
.
I only want to call up help on the command cd
alone. Therefore I would need to type just cd --help
and I'll get the help page up for that command.
Man Page
man <COMMAND>
View the man page (short for manual page) of a given command or application. This is the Linux equivalent of requesting the instruction booklet and can help give you detailed information about the command you are trying to run.
General Commands
You'll use these all the time to navigate around the file system, execute commands and generally get used to Linux.
sudo <COMMAND>
Placing the term sudo before a command ensures that it gets executed as an elevated user. You will find that a lot of commands require you to put sudo before them because they need to write to the file system or perform special operations that only an elevated user should be able to perform. Once you ask to execute a command as an elevated user you'll usually need to provide the password.
~
This is shorthand for the current user's home directory, which is usually /home/ or /root/ if you are logged in as root. You can use it in place of /home/ as a shorthand or if you are writing a script where you don't know the user's username up front.
cd <DIRECTORY>
Changes the current working directory to the new directory. e.g. cd /etc/
navigates to the /etc/ directory.
pwd
Shows your current directory location on the file system. It stands for 'path of working directory'.
&&
This can be used to chain multiple commands together in a single line. They will then get executed in order.
|
Piping is important in Linux. It allows you to take the output from one command and use it as the input for another command. This is especially useful when dealing with files. You can output the file using cat <FILENAME>
then pipe it to another program, such as grep
for searching it. For instance cat phone-numbers.txt | grep Dave
. This will then search each line of the file phone-numbers.txt
searching for the word Dave
.
Every line it finds that matches is output to the terminal and can even be piped again to search the results even further.
clear
Clears the terminal screen.
watch
Allows you to update the terminal in realtime with the output of a command. Useful for combining with memory usage commands to view changes.
nano <FILENAME>
A simple text editor.
date
Outputs the current date and time.
Viewing Files
cat <FILENAME>
Outputs the contents of the file to the terminal. Used for viewing the contents of a file quickly.
more <FILENAME>
or <COMMAND> | more
Can be used to view the contents of a large file or stream, allows piping. Only allows you to go down through the file.
less <FILENAME>
or <COMMAND> | less
Can be used to view the contents of a large file or stream, allows piping. Allows you to use page up and page down keys to move up and down.
head <FILENAME>
Shows you the first 10 lines of a file.
head -n <NUMBEROFLINES> <FILENAME>
Shows you the first X number of lines of a file.
tail <FILENAME>
Shows you the last 10 lines of a file.
tail -n <NUMBEROFLINES> <FILENAME>
Shows you the last X number of lines of a file.
tail -f <FILENAME>
Shows you the last lines of a file and follows the file, showing any updates that are made to it. Useful for watching the realtime output of a log file.
grep <SEARCHTERM> <FILENAME>
or <COMMAND> | grep <SEARCHTERM>
Used to search a file or piped stream for the given search term. Extremely important and powerful. This is the case-sensitive version. For case-insensitive search add the -i
flag like so: grep -i <SEARCHTERM> <FILENAME>
or <COMMAND> | grep -i <SEARCHTERM>
.
Updating and Upgrading
Ensuring your server is up to date with the latest software and patches is essential. Ubuntu uses Aptitude as it's package manager so all of these commands are centered around that. If you use another Linux distribution it's worth checking the package manager that comes with it for commands.
apt-get update
Updates your repositories. Run this before performing any of the other commands.
apt-get upgrade
Installs the latest versions of all packages.
apt-get dist-upgrade
Upgrades the current
apt-get autoremove
Will remove orphan dependencies that are no longer referenced by applications on the system. These are basically just taking up space on the disk, it's good to clean up.
/etc/apt/sources.list
Contains a listing of the repositories configured for apt, sometimes you might need to manually add repositories to this file in order to install some additional software.
Maintenance
Memory
free -h
Shows the memory utilisation in an easy to read format.
vmstat -s
Shows the memory utilisation in an easy to read format.
Process Information
top
Shows CPU and memory utilisation of processes running on the system.
Disk Space
df -h
Displays disk and partition usage in an easy to read format.
Security
Authentication
last
Shows the last successful login attempts.
last -F
Shows the last successful login attempts with timestamp information.
last /var/log/wtmp.1
Show the last successful login attempts older than a month.
lastb
Shows the last unsuccessful login attempts.
w
Show currently logged in users.
/etc/passwd
This file contains all the users registered on the system.
whoami
Shows who you are currently logged in as.
passwd -aS
Shows you current users for the system and whether passwords are assigned. An unassigned password means the user cannot log in.
Firewall and Ports
Ubuntu comes with UFW as the standard firewall, it's relatively easy to use and configure, hence the name it stands for which is 'uncomplicated firewall'.
ufw status
Show the current status of the firewall, whether it's enabled or disabled and what rules you have in place.
ufw deny <PORTNUMBER>/<PROTOCOL>
Blocks traffic for a given port number and protocol (tcp or udp).
ufw allow <PORTNUMBER>/<PROTOCOL>
Allows traffic for a given port number and protocol (tcp or udp).
ufw limit <PORTNUMBER>/<PROTOCOL>
Limits traffic for a given port number and protocol (tcp or udp).
/etc/hosts.deny
Location for blocking IP addresses from accessing the system.
nc -zv <IPADDRESS> <PORTNUMBER>
Checks whether a port is open at a given IP address. Useful for debugging firewall configurations.
netstat -uplant
Discover what ports applications are listening on.
Remote Access
ssh <USERNAME>@<IPADDRESS>
Opens a secure shell (terminal) to a remote machine via the SSH protocol. You can also specify the port with the -p
flag, like so: ssh <USERNAME>@<IPADDRESS> -p 1000
.
service sshd restart
Restarts the SSH Daemon which reloads any configuration changes made to the /etc/ssh/sshdconfig file.
Information
whois <IPADDRESS>
Performs a whois lookup for the given IP address, giving geographical information.
Useful Files
~/.bash_history
File contains the user's command history.
~/.profile
File contains commands that are run at the start of new login shells.
~/.bashrc
File contains commands that are run at the start of each new bash session.
/etc/ssh/sshd_config
Location for the server's SSH configuration.
/var/log/ufw.log
File contains logs from the firewall application ufw.
/etc/services
Shows the port/protocol mapping for services that can be used on the system.
/proc/meminfo
Shows memory allocation.