Elevating Privileges

Learn how to use sudo privileges in the CLI.

We'll cover the following

We have complete and total control over all of the files in our home directory. But we don’t have free rein over directories and files across the whole disk. We can do certain things only with superuser privileges. On Linux, Unix, BSD, and macOS systems, this is called the root user. To keep things more secure and prevent accidents, regular user accounts are restricted from modifying things outside their home directories.

Try to create a new directory called /var/website:

$ mkdir /var/website

This command will fail with the following error:

mkdir: /var/website: Permission denied

We’re not allowed to create files in the /var directory. Only certain users can do that. But thanks to the sudo command, we can execute a single command as the root user without logging in as that user. To use it, prefix the previous command with sudo, like this:

$ sudo mkdir /var/website

The sudo command

Think of this sudo command as “superuser do mkdir /var/website.” The command will complete successfully, and we can verify that it exists by using the ls command to view the contents of the /var directory:

$ ls /var/

backups cache crash lib local lock log mail metrics opt run snap spool tmp website

The website directory is now listed in the output.

Try implementing this on your machine since these directories are not visible in the terminal provided here.

The sudo command is powerful but dangerous. We’re running the command as a privileged user, so if the command does something sinister, we could be in a lot of trouble. It also bypasses any file permission restrictions, meaning we could accidentally alter or delete files owned by anyone if we accidentally run the wrong command. Use this with care!

One place we’re likely to use sudo is when modifying system-wide configuration files or, as we’ll try next, installing additional programs or tools on our operating system.

Since the machines on our platform already have superuser privileges, you should try these commands locally on your machine.

Get hands-on with 1200+ tech skills courses.