Saturday 21 September 2019

What You Should Know About the Sudo Command

LPI Certification, LPI Guides, LPI Tutorials and Materials, LPI Guides, LPI Sudo Command, LPI Online Exam

New users to Linux (especially Ubuntu) eventually become aware of the Sudo command. Many users never use it for anything other than getting past "permission denied" messages — but Sudo does so much more.

LPI Certification, LPI Guides, LPI Tutorials and Materials, LPI Guides, LPI Sudo Command, LPI Online Exam

About Sudo


A common misconception about Sudo is that it is used solely to provide root permissions to an ordinary user. In fact, the Sudo command allows you to run a command as any user, with the default generally being the root.

How to Grant User Sudo Permissions


Ubuntu users typically take the ability to run the Sudo command for granted. That's because, during installation, a default user is created, and the default user in Ubuntu is always set up with Sudo permissions. If you are using other distributions or have other users within Ubuntu, however, the user likely needs to be granted permissions to run the Sudo command.

Only a few people should have access to the Sudo command, and they should be system administrators. Users should be given only the permissions they need to perform their jobs.

To grant users Sudo permissions, you just need to add them to the Sudo group. When creating a user, use the following command:

$sudo useradd -m -G sudo

The above command will create a user with a home folder and add the user to the Sudo group. If the user already exists, then you can add the user to the Sudo group using the following command:

$sudo usermod -a -G sudo

A Neat Sudo Trick for When You Forget to Run It


Here's one of those terminal command tricks you can learn from seasoned experts — in this case, for getting past the "permission denied" message. If it's a long command, you can go up through the history and put Sudo in front of it, you can type it out again, or you can use the following simple command, which runs the previous command using Sudo:

$sudo !!

How to Switch to Root User Using Sudo


The Su command is used to switch from one user account to another. Running the Su command on its own switches to the superuser account. Therefore, to switch to the superuser account using Sudo, simply run the following command:

$sudo su

How to Run a Sudo Command in the Background


If you want to run a command that requires superuser privileges in the background, run the Sudo command with the -b switch, as shown here:

$sudo -b

Note that, if the command being run requires user interaction, this won't work.

An alternative way to run a command in the background is to add an ampersand to the end, as follows:

$sudo &

How to Edit Files Using Sudo Privileges


The obvious way to edit a file using superuser privileges is to run an editor such as GNU nano, using Sudo as follows:

$sudo nano

Alternatively, you can use the following syntax:

$sudo -e

How to Run a Command as Another User Using Sudo


The Sudo command can be used to run a command as any other user. For example, if you are logged in as user "john" and you want to run the command as "terry," then you'd run the Sudo command in the following way:

$sudo-u terry

If you want to try it out, create a new user called "test" and run the following Whoami command:

$sudo -u test whoami

How to Validate Sudo Credentials

When you run a command using Sudo, you'll be prompted for your password. For a period afterward, you can run other commands using Sudo without entering your password. If you wish to extend that period, run the following command:

$sudo -v

Related Posts

0 comments:

Post a Comment