Basic Ubuntu Linux: sudo and apt-get

Source: https://xkcd.com/149/

The power of Linux lies in the strength of it’s terminal and the ability to process complex commands. New users may be a bit intimidated by the Linux terminal, but here are two of the most basic commands you’ll need to know to do anything productive at a Linux shell.

Table of Contents

sudo

In Windows, it is an unfortunately common mistake to have every user on a machine set up as a member of the Administrators group. The reason this is a mistake is because Administrators have access to parts of the system that when modified or deleted, can severely cripple a machine. The same rule holds true in Linux.

In Linux, another name for an Administrator is the root user or superuser. The superuser account exists primarily for system administration purposes, and should only be used when performing system administration tasks such as:

  1. Software installation.
  2. Configuration file modification (opening a system settings file, editing it, and saving it).
  3. Server control (starting/stopping daemons/services).
  4. Permissions modification (changing access rights on individual files/folders).

In Ubuntu Linux, the default user account does not have superuser access. The only way to run a command that requires superuser access is to prefix it with a command called sudo.

sudo stands for “substitute user do”, and is appropriately pronounced like “pseudo” (a Greek prefix meaning, “Pretending to be something it is not”).

The moral of the story is, if we want to do anything in the above list on an Ubuntu Linux machine using the terminal, we’ll probably need to prefix our command with sudo. This is telling the computer, “Yes computer, I know I’m not an administrator/superuser (or another user), but I want to do something that requires administrator/superuser/that other user’s access.”

Keep this in mind. On to our next command…

apt-get

This guy won't be awesome until he uses sudo.

In Windows, whenever we want to install an application, it usually involves a few steps:

  1. Download an installer such as an MSI or EXE file. It may be contained within a ZIP file.
  2. If the installer is in a ZIP file, extract/unzip the installer.
  3. Run the installer.
  4. Answer a series of questions about what you’re installing.
  5. Watch a little progress bar while hoping it arrives at 100% completion without any fuss.

In Linux, things can get even more complex depending on what we’re trying to accomplish, but fortunately there are tools that aim to make the most common software installs simple. One of such tools that can be found on all Ubuntu Linux distributions is apt-get. apt-get accepts a lot of different arguments, but the most commonly used argument is install, like so:

regularuser@smalleycreative:~$ apt-get install [name of program]

Question: apt-get is a tool that among other things, is primarily used to install software. If you’ve been following along up until now, what does the fact that we’re installing software immediately tell you?

Answer: In order to use apt-get, we need to have superuser access. How do we get superuser access if we’re not using a superuser account? We prefix the command with sudo. If we try to use apt-get to install a program like VLC Media Player, and we don’t prefix the command with sudo, we can expect an error message:

regularuser@smalleycreative:~$ apt-get install vlc
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Now that you’ve read the explanation, the above errors (the lines that start with E) should make a lot of sense. We are being told that we can’t install VLC Media Player, and we’re also given a very possible reason why. Let’s look again:

regularuser@smalleycreative:~$ apt-get install vlc
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

“Permission denied.”

“Are you root?”

Linux gets a lot of flak for not holding users’ hands, but this is actually a notably helpful series of error messages with hints to help us overcome them. We need to use the sudo prefix. Let’s try it:

regularuser@smalleycreative:~$ sudo apt-get install vlc
[sudo] password for regularuser:

We’re prompted for our password, and upon entering it, the apt-get command runs through it’s normal install procedures:

regularuser@smalleycreative:~$ sudo apt-get install vlc
[sudo] password for regularuser:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
wwwconfig-common libmcrypt4 libjs-mootools mlock libc-client2007b javascript-common
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
libass3 libdca0 libdvbpsi5 libebml0 libenca0 libiso9660-5 liblua5.1-0 libmatroska0 libqtcore4 libqtgui4 libtar libupnp3 libvcdinfo0 libvlc2 libvlccore2 vlc-data vlc-nox vlc-plugin-pulse
Suggested packages:
qt4-qtconfig mozilla-plugin-vlc videolan-doc
The following NEW packages will be installed:
libass3 libdca0 libdvbpsi5 libebml0 libenca0 libiso9660-5 liblua5.1-0 libmatroska0 libqtcore4 libqtgui4 libtar libupnp3 libvcdinfo0 libvlc2 libvlccore2 vlc vlc-data vlc-nox vlc-plugin-pulse
0 upgraded, 19 newly installed, 0 to remove and 60 not upgraded.
Need to get 17.7MB of archives.
After this operation, 51.8MB of additional disk space will be used.
Do you want to continue [Y/n]?

If we agree to continue, VLC Media Player gets installed on our Ubuntu Linux machine.

This concludes this lesson. I hope you enjoyed it as much as I enjoyed putting it together.

UPDATE: Coincidentally, an article about configuring the sudo command for use in other Linux distributions (beyond Ubuntu) was added to Lifehacker earlier today.


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *