- How to switch between users on one terminal?
- 10 Answers 10
- How to Change Users in Linux Command Line
- Various user types in Linux
- 1. System Users
- 2. Regular Users
- 3. Super Users
- Switch users in the command line
- Switch to root user
- Linux change user sudo
- The sudo — Super User Do
- Using sudo
- Examples
- Changing the sudo Configuration
- The su command
- References & Resources
- Latest Post
How to switch between users on one terminal?
I’d like to log in as a different user without logging out of the current one (on the same terminal). How do I do that?
I asked a similar question, because I ran into issues with XDG environment variables ( $XDG_RUNTIME_DIR in particular) that were driving me nuts. -> unix.stackexchange.com/questions/354826/…
10 Answers 10
How about using the su command?
$ whoami user1 $ su - user2 Password: $ whoami user2 $ exit logout
If you want to log in as root, there’s no need to specify username:
$ whoami user1 $ su - Password: $ whoami root $ exit logout
Generally, you can use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:
$ whoami user1 $ sudo -u user2 zsh $ whoami user2
There are more circuitous ways if you don’t have sudo access, like ssh username@localhost, but sudo is probably simplest, provided that it’s installed and you have permission to use it.
I am getting this error «-su: /dev/stderr: Permission denied» after executing this command echo >>/dev/stderr on a login with su —login . , any tip? I found this btw unix.stackexchange.com/questions/38538/…
Does this allow each new user to have different, overriding values for environment variables? e.g. git config for work, open source, etc.
One finding, when I listed the env it saw that everything was in order as well as a visual inspection can go; And one thing was incorrect: XAUTHORITY=/home/user1/.Xauthority’. Not sure _why_? So X-window doesn’t work by default because the protection on ~/.Xauthority` file is: -rw——- . I made a copy and that let me run gedit as an experiment.
Generally you use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:
[mrozekma@etudes-1 ~] % whoami mrozekma [mrozekma@etudes-1 ~] % sudo -u nobody zsh [nobody@etudes-1 ~] % whoami nobody
There are more circuitous ways if you don’t have sudo access, like ssh username@localhost , but I think sudo is probably simplest if it’s installed and you have permission to use it
sudo -s gives you a shell like su , sudo -i simulates login like su — . Can be combined with -u $user , of course.
much more efficient. being able to «login» as a user who can’t normally login is a great asset. totally allowed me to run a database instance without messing with permissions or selinux
This command prints the current user. To change users, we will have to use this command (followed by the user’s password):
After entering the correct password, you will be logged in as the specified user (which you can check by rerunning whoami .
If you’re running Ubuntu, and if the user you want to login as doesn’t have a password set:
Enter your own password and you should be set. Of course, this requires that your user has rights to gain root privileges with sudo .
To switch the terminal session to a different user, where that user can’t exit back into the original user, use exec:
This will technically login the new user in a new term process, and close out the current one. That way when the user attempts exit or Ctrl-D, the terminal will close as though that user was the one who instantiated it, i.e., the user can’t exit back into the original user’s term. Kind of pointless, considering they can still just start a new terminal session and automatically be in the original user term login, but there it is.
EDIT: For what it’s worth, you can use linux vlock command in your ~/.bashrc to lock terminal sessions by default, requiring the password of the term session user to unlock. This would somewhat prevent the aforementioned term restart under the original user context, given the term isn’t instantiated using the non-default ~/.bashrc of the user, as configured.
How to Change Users in Linux Command Line
Linux systems have different users and different types of users. Learn how to switch the users in Linux command line or how to change to the root user in Linux.
Linux systems have different types of users with different types or permissions as well.
Not all users can execute all commands and not all users are allowed to switch to other users neither. This all might sound confusing but, I will try to explain these so it can be easy to understand.
For the moment, here’s a quick summary of how to switch users in Linux command line.
To switch users, you need to know the password of that user. You can switch the users with this command:
To switch to root user in Ubuntu, you can use this command:
Various user types in Linux
If you list all users in Linux, you’ll see a lot of users that you didn’t know about. Who are these users? Where did they come from? I could write an entire article in regards of how users work in Linux, however, this is not the idea for this one.
Basically, there are 3 types of users in Linux:
1. System Users
These are the users that are automatically created in Linux systems to be able to run services or applications and are not intended to log in to the system (in fact you can’t log in as any of these users).
2. Regular Users
These are the (human) users who can log in to a system. Each of these users might have or not different permissions or levels in the system which is given by the groups they belong to.
3. Super Users
These are system administrators or users who can perform high-level tasks that can be considered critical or system dangerous.
Switch users in the command line
When using a Linux system you can log in with a user and then simply “switch” to another user through the same command line session. In order to do this, there is a command “su -“, which allows you to switch to become another user:
[email protected]:~$ su - janedoe Password: [email protected]:~$
In the above example, you need to know the password of janedoe in order to switch to that user. Which makes sense because if you are going to switch to a user, you need to know the password of that user else it will be a security risk.
Switch to root user
For security reasons, some systems have ‘root’ account blocked for direct login, either locally or remotely, so this means it will not accept someone who tries to log in using ‘root’ even with the correct password.
So, how do you perform actions as the ‘root’ user? That’s what the ‘sudo’ command allows you to.
The sudo command will basically execute anything you want in the system as if the ‘root’ was doing it. You don’t need to know the ‘root’ user’s password, in fact, probably nobody knows it or there is no password assigned to ‘root’. You only need to know your own user’s password and that user must be in the ‘sudoers’ group, which is basically the group of users which can use ‘sudo’ in the system.
Normally, it is a good practice to run the commands with sudo that needs to run with root permission like this:
But if you want to change to root user so that all the subsequent commands will be run as root, you can use:
You’ll use your own password here, not the root account’s password.
As a sudo user yourself, you can create sudo user by adding the user to sudo group.
Linux systems allow you to easily switch users or execute high-level commands with the usage of ‘su‘ and ‘sudo’ commands. And remember: with great sudo power comes great responsibility!
Linux change user sudo
This lesson will cover how to switch to other accounts using sudo command.
The sudo — Super User Do
Another way to switch users or execute commands as others is to use the sudo command. The syntax for sudo is:
The 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.
The 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.
Using sudo
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 .
Commands | Meaning |
---|---|
sudo -l | List available commands. |
sudo command | Run command as root. |
sudo -u root command | Run command as root. |
sudo -u user command | 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.
Commands | Meaning |
---|---|
sudo su | Switch to the superuser account. |
sudo su — | Switch to the superuser account with root’s environment. |
sudo su — username | 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 -u option.
Commands | Meaning |
---|---|
sudo -s | Start a shell as root |
sudo -u root -s | Same as above. |
sudo -u user -s | Start a shell as user. |
Examples
You can run the following to see what command can be run with sudo :
$ 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
$ sudo /etc/mongodb start mongodb started
To run a command as the other user:
$ sudo -u nikki /etc/nikkiapp/bin/start Nikki's app started Running as user: nikki
To change to root account:
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
admin ALL=(ALL) NOPASSWORD:ALL
robin servername=(root) /etc/init.d/apps
The su command
Another way to switch user is to use su command. The details of su is covered in su command.
References & Resources
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- 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
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync