Switching user in linux

How to Use the su Command in Linux with Examples

Home » SysAdmin » How to Use the su Command in Linux with Examples

In Linux, the su command (switch user) is used to run a command as a different user.

In this guide, you will learn how to use the su command, with practical examples.

  • A system running Linux
  • Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)

How the su Command Works

The su command is used to run a function as a different user. It is the easiest way to switch or change to the administrative account in the current logged in session.

Some versions of Linux, like Ubuntu, disable the root user account by default making the system more secure. But, this also restricts the user from running specific commands.

Using su to temporarily act as a root user allows you to bypass this restriction and perform different tasks with different users.

Note: A root account is a master administrator account with full access and permissions in the system. Because of the severity of changes this account can make, and because of the risk of it being compromised, most Linux versions use limited user accounts for normal use.

su Command Syntax

To use the su command, enter it into a command-line as follows:

If a username is specified, su defaults to the superuser (root). Simply find the user you need and add it to the su command syntax.

su Command Options

To display a list of commands, enter the following:

Here are some common options to use with the su command:

  • Username – Replace usernamewith the actual username you want to log in with. This can be any user, not just root.
  • –c or –command [command] – Runs a specific command as the specified user.
  • or –l or –login [username] – Runs a login script to change to a specific username. You’ll need to enter a password for that user.
  • –s or –shell [shell] – Allows you to specify a different shell environment to run in.
  • –h or –help – Show the help file for the su command.
  • –p or ––preserve–environment – Preserve the shellenvironment (HOME, SHELL, USER, LOGNAME).

su Command Examples

Switch to a Different User

To switch the logged-in user in this terminal window, enter the following:

You’ll be asked for a password. Enter it, and the login will change to that user.

If you omit a username, it will default to the root account. Now, the logged-in user can run all system commands. This will also change the home directory and path to executable files.

Use the whoami command to verify you switched to a different user.

Note: If you are having issues with authentication, you can change the root or sudo password in a couple of simple steps.

Run Specific Command as a Different User

To run a specific command as a different user, use the –c option:

The system will respond by asking you for the user password.

When you enter this example, the system will use the specified account to run the ls (list directory contents) command.

Читайте также:  Windows update agent server 2012

Use a Different Shell

To use a different shell, or operating environment, enter the following:

This command opens a root user account in Z shell.

Use a Different User in the Same Environment

You can keep the environment of the current user account with the –p option:

Replace [other_user] with the actual username you want to switch to.

The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.

To verify you remained in the same home environment, use the echo $HOME command that will display the directory you are working in.

Command Comparison: su vs sudo

sudo Command

The sudo command grants a one-time or limited-time access to root functionality. Typically, the sudo command is used to quickly run an administrative command, then return to the user account’s regular permissions.

To provide sudo access, the user has to be added to the sudo group.

Note: By default, some versions of Linux (such as Ubuntu) disable the root account. That means there’s no password assigned to the root user. However you can switch to root by running the following command and entering the currently logged-in user’s password:

su Command

The su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account. Additionally, su can also be used to change to a different shell interpreter on the fly.

su is an older but more fully-featured command. It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.

You now know how to use the su command to temporarily change users and execute commands in Linux. Use the examples provided to get started.

Источник

How to Change Users in Linux Command Line

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:

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.

Читайте также:  Latest usb driver windows zip

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.

Conclusion

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!

Источник

How to Switch Users in Ubuntu and Other Linux Distributions [Quick Beginner Tip]

Last updated October 29, 2020 By Abhishek Prakash 12 Comments

It is really simple to switch users in Ubuntu or any other Linux distribution using the terminal.

All you need to do is to know the unsername and its account password and you can switch users with su command:

You’ll be asked to enter the password of the user you want to switch to.

As you can see in the screenshot above, I changed to user prakash from user abhishek in the terminal.

There are some minor details with this method that I’ll share with you in a moment. I’ll also share the graphical way of switching users in Linux if you are using desktop Linux.

Switching to root user

If you want to switch to the root user in Ubuntu, you can use the following command:
sudo su
You’ll have to enter your own user password here.

Change user in Linux command line

Let’s see things a bit in detail. To switch users, you need to first know the exact username because tab competition doesn’t work here. You can list all the users in Linux command line by viewing the content of the /etc/passwd file.

You’ll also need to know the password of the user account you want to switch to. This is for security reason, of course.

If you are the admin user or have sudo access, you can change account password with passwd command.

You’ll notice that some people use a — between su and the username. There is a specific reason for that.

When you use -, -l or –login option, you start the shell as a login shell. This means that it will initialize the environment variables like PATH and changes to the home directory of the changed user. It will be as if you logged into the terminal as the second user.

Note: though — is more popular, it is advised to use –login option.

Change users in Linux graphically (for desktop Linux)

If you are using desktop Linux, the above method may not be sufficient for you. Why? Because you switch the user in the terminal only. It is confined to the terminal. Nothing is changed outside the terminal.

If you want to switch users so that you can log in as another user and use all the system (browser, applications etc) graphically, you’ll have to log out and then log back in.

Читайте также:  Сравнение файловых систем linux windows

Now the screenshots may look different but the steps remain the same. Here’s how to switch users in Ubuntu Linux.

Go to the top right corner and click the Power Off/Log out option to open the dropdown and you can choose either of Switch User or Log Out.

  • Switch User: You get to keep your session active (applications keep on running) for current user. Good for temporarily switching users as you won’t lose your work.
  • Log out: Current session ends (all applications are closed). Good when you want to switch to the other user for a long time.

You can choose whichever option is more suited for your need.

Now, you’ll be at the login screen with all the available users for your system. Choose the user account of your choice.

Clearly, you need to know the password of the user account you want to use.

That’s it. I hope you find this quick beginner tip helpful in changing users in Ubuntu and other Linux distributions. Questions and suggestions are always welcome.

Like what you read? Please share it with others.

Источник

How to Become Root or Any Other User Using the Linux Command Line

The subsitute-user command allows easy access to other user accounts

What to Know

  • To switch to the root user on Ubuntu-based distributions, enter sudo su in the command terminal.
  • If you set a root password when you installed the distribution, enter su.
  • To switch to another user and adopt their environment, enter su — followed by the name of the user (for example, su — ted).

This article explains how to switch users on Linux using the sudo command.

Switch to the Root User

The way you switch to the root user differs by distribution. For example, on Ubuntu-based distributions such as Linux Mint, Ubuntu, Kubuntu, Xubuntu, and Lubuntu, switch using the sudo command as follows:

If you are using a distribution which allowed you to set a root password when you installed the distribution then you can simply use the following:

If you ran the command with sudo then you will be asked for the sudo password but if you ran the command just as su then you will need to enter the root password.

To confirm that you have indeed switched to the root user type the following command:

The whoami command tells you which user you are currently running as.

Switch to Other Users and Adopt Their Environment

The su command switch to any other user’s account. This ability is useful when you’re testing user-account provisioning.

For example, assume you created a new user called ted using the useradd command. Switch to the ted account using the following command:

As it stands, the above command would log you in as ted but you wouldn’t be placed in the home folder for test and any settings that ted has added to the .bashrc file will not be loaded.

You can, however, log in as ted and adopt the environment using the following command:

This time when you log in as ted, you will be placed into the home directory for ted.

Execute a Command After Switching User Accounts

To switch to another user’s account but have a command run as soon as you switch, use the -c switch as follows:

su -c screenfetch — ted

In the above command, su switches user, the -c screenfetch runs the screenfetch utility and the — ted switches to the ted account.

What Is Su?

“Su” stands for substitute user. The sudo command runs any command as another user account and is commonly used to elevate permissions so that the command is run with elevated security privileges (which in Linux terms is known as the root user). Sudo works for a brief period of time. To run as another user for a prolonged period of time, use the su command.

Источник

Оцените статью