Basic Ubuntu Linux: sudo and apt-get

The power of Linux lies in the strength of the terminal’s 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 terminal.

sudo

sudo - From xkcd.com

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 a root user or a  superuser.  The superuser account exists solely 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 and editing it).
  3. Server control (starting/stopping daemons/services).
  4. Permission 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 (you guessed it) “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, but I want to do something that requires administrator/superuser 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 the installer or find the install media.
  2. If necessary, extract/unzip the installer.
  3. Run the installer.
  4. Answer a series of questions about what you’re installing.
  5. Wait.

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 VLC Player, a popular media player, and we don’t prefix the command with sudo, we get an error message like this:

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 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?”

So our good friend Linux just pointed us in the right direction.  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 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 (besides Ubuntu) added to Lifehacker earlier today.  They even used the same cartoon from xkcd.  I guess I share the same sense of humor with the folks over at Lifehacker.

Michael is the creator and main author of the Smalley Creative Blog. He is a guy who enjoys technology (particularly open source), educating people about technology, and working with people who enjoy technology as much as he does. Follow him on Twitter @michaeljsmalley.

Leave a reply:

Your email address will not be published.

Site Footer