I use Ubuntu servers all the time, and there are some commands that everyone that uses them should know. I have compiled a list of what I think are the most useful commands a Ubuntu user can know. They most likely work on most other Linux systems but I haven't tested them.
Before we get started
There are some useful things to know about the command line before you get started with the below. These aren't exactly commands but are used within commands.
/is the root directory of your system. Its used in directory paths.
~is your home directory.
\will escape a character for you. For example whitespace, if you have a space in a directory name you will need to use this before the space. E.g.
Directory and file commands
Lets start with some basics, the
ls command simply lists the current working directory structure. You will probably also want to know some of the
ls option keys. The most useful I find are
-l, '-a' and
-h which do the following:
a- will show hidden files as well
l- will show files in a list format
h- will show the size of the files in human readable format.
You can also view the structure of a different directory by stating the directory after your options. For example
ls -lah downloads/movies/
will list the files in the
No good doing anything on command line unless you can move about within the directory structure.
cd will change directory.
will move you into the movies directory.
Sticking with the directory theme,
mkdir will make a directory.
will make a directory called movies in your current working directory. You can specify to make a directory in a different location by concatenating the location in front of the new one.
will make a directory called movies in the downloads directory.
rm will remove your unwanted files or directories
will remove the index.html file in your current location. You may also need some of the flags for
- '-f' - will attempt to force the removal regardless of permissions and without prompting for confirmation
- '-R' - will remove recursively, meaning it will remove a directory and its contents including other directories.
rm -Rf /downloads/movies
will recursively remove the movies directory and all of its contents without prompting for confirmation. Now you will understand the jokes and/or scams trying to get you to run
rm -Rf / on your system (dont run it). This will remove your entire directory structure given that
/ is your root directory.
This is another useful directory/file based command.
mv will move something for you.
mv index.html var/www/
will move the
index.html file into your
var/www directory. Another use of the
mv command though is to also rename a file.
mv index.html index_old.html
If you dont want to move something, but rather copy it then
cp is the command you want.
cp index.html var/www
will copy your
inde.html file into the
cat will print out the contents of a file to your screen.
will print out your public SSH key to the screen.
Moving on from directories now we have
restart which I personally normally use in conjunction with
restart command will restart your system. Used with
service it will just restart a service. Taking Nginx for example
service nginx restart
will restart the
nginx service. You will need that one a lot when you are configuring Nginx
However, sometimes you dont want to restart a service. Maybe you just want to reload the config.
service nginx reload
will do just that. The difference being that
reload will continue the service running, a
restart will stop the service, then start the service again. Giving a momentary downtime.
You probably wont need this one if you are just running a basic website or something on your server. However it can become quite useful if you have multiple sites or a small disk.
df will tell you how much space is being used and how much is free on your disk. Similar to the
ls command above, you will likely want the
-h flag for humanly readable.
top will bring up a screen with your currently running process, how much memory and CPU they are using among other things. This can be useful if you are having issues and need to diagnose some problems like memory leaks.
This is used to get the contents of a url. It has many uses, i mainly use it for 2 specific reasons. Either I simply want to download the contents or I want to run the script that is at that location.
this will download the iso from that location.
this will run the
sendNotification.php script. Which could be run on say a cron job.
apt-get is a essential tool on a Ubuntu server. There are three parts to it,
update. This is what you will use to install all your packages like Nginx or MySQL.
this will update the list of repositories in the apt get lists so you are install the most up-to-date stable releases.
apt-get install nginx
this will install the
apt-get remove nginx
this will remove the
Permissions and user commands
When it comes to installing things on your system, its likely you will need to be a root user. Or at least have root level permissions for the system.
sudo will ensure you are running your commands with those privileges. This will in most cases then ask for your password.
sudo apt-get install nginx
Talking of permissions, there may come a time when you need to change these.
chmod will change the permissions levels for the given file. Its too in depth to explain Linux permissions here, however I will give a brief overview.
Permissions on a Ubuntu (Linux) system are based on a numbering system. With 3 classes of user. The
group and the
owner is the user than owns the file/directory. The
group is the group of users that the directory belongs to and the
world users are everyone else.
There are then 3 levels of permissions, read (
r), write (
w) and execute (
x). There are fairly self explainatory. Each of these permissions is given a point level and those points added together for each user and then concatenated give the file/directory its permission level.
- 4 points for
- 2 points for
- 1 point for
index.php for example. If I want
x for the owner,
w for the group and just
r for everyone else my persmissions would be as follows:
4 + 2 + 1 = 7 # owner 4 + 2 = 6 # group 4 = 4 # world
Concatenate those numbers together you get
764 so running
chmod 764 index.php
will give those permissions for those users for the index.php file.
Another useful note here is the
-R command is used for recursion through directories.
This will chnage the owner of the file / directory.
chown john app/
This will make john the owner of the app directory.
This will change the group of the file / directory.
chgrp www-data app/
This will change the
app directory group to www-data
Not much good chnaging permissions for users if you dont have any. The
adduser command will do just that. Some people like to user
useradd which technically does the same thing, but
adduser will guide you through adding the user with passwords, names etc and create the home directory for you.
useradd wont, you will need to do that manually.
You will need this one to add an existing user to a group.
user mod www-data john
this will add the user john to the group www-data.
This unsurprisingly will remove a user.
Will remove the user john from the system.