Switching user using sudo
This lesson will cover how to switch to other accounts using
sudo - Super User Do
Another way to switch users or execute commands as others is to use the
sudo command. The syntax for
sudo allows you to run programs with the security priviledges of another user. Like
su, if no username is specified, it assumes that you were trying to run commands as the superuser. This why
sudo is referred to as superuser do.
It is commonly used to install, start and stop applications that require root priviledges. One of the advantages of using
sudo over the
su command is that you don't need to know the password of the other user. This could eliminate some issues that arise from using shared passwords in generic accounts. When you execute the
sudo command, you are prompted for your password. If the
sudo configuration permits access, that command is executed.
sudo configuration is typically controlled by the system administrator and requires root access to change. Of course, on your personal system, you have access to the root account, and you are effectively the system administrator as well.
To see the commands that are available for you to run with sudo, use
sudo -l. To run a command as the root user, use
sudo command. You can specify a user with
-u, for example
sudo -u root command is the same as
sudo command. However, if you want to run a command as another user, you need to specify that with
-u. So, for example
sudo -u nikki command.
||List available commands.|
||Run command as root.|
||Run command as root.|
||Run command as user.|
You can use
sudo su to switch to the superuser account. You can use
sudo su - to switch to the superuser account with root's environment. The
sudo su - username would switch to that username's account with an environment that you would expect to see when you logged in as that user.
||Switch to the superuser account.|
||Switch to the superuser account with root's environment.|
||Switch to the username's account with the username's environment.|
Another way to switch to another account with
sudo is to use the
-s option. If you run
sudo -s that will start a shell as root. You can specify a user with the
||Start a shell as root|
||Same as above.|
||Start a shell as user.|
ExamplesExample 1: What command to run with sudo.
You can run the following to see what command can be run with
$ sudo -l Matching Defaults entries for robin on robin-ThinkPad-T410: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin User robin may run the following commands on robin-ThinkPad-T410: (ALL : ALL) ALL
In this case, user robin can run any command.Example 2: Run a command with sudo
Run a command with
$ sudo /etc/mongodb start mongodb startedExample 3: Run a command as the other user
To run a command as the other user:
$ sudo -u nikki /etc/nikkiapp/bin/start Nikki's app started Running as user: nikkiExample 4: Change to root account
To change to root account:
$ sudo -s $ whoami root
Changing the sudo Configuration
If you need to modify the
sudo configuration, use the command
visudo. It effectively starts the vi editor and edits as the /etc/sudoers file. The
visudo command has to be executed with approved priviledges. This means that you need to switch to the root account, for example:
su root visudo or run
sudo visudo as your own account.
The sudoers file contains a list of users, and what commands that those users can run and as what users those commands can be run as.
There are many options to the sudoers file, but the most simple format and one that you will see quite often is a line that gives a specific user a set of commands that he or she can run. The format is like
user host=(users) [NOPASSWORD:]commands
Here are 2 examples:
admin ALL=(ALL) NOPASSWORD:ALL
robin servername=(root) /etc/init.d/apps
Another way to switch user is to use
su command. The details of
su is covered in su command.
References & Resources
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync