How to Change the User in Bash

Sheeraz Gul Feb 02, 2024
  1. Bash Change the User
  2. Use the su Command to Change the User in Bash
  3. Use the sudo Command to Change the User in Bash
  4. Change the User to Root in Bash
How to Change the User in Bash

This tutorial demonstrates how to change the user in Bash.

Bash Change the User

There are different methods to change the user in Bash. We can use the su command or sudo command to change the user directly or switch to the root user using a method.

To change a user in Bash, first of all, we need to know the names of users in the environment. To list the names of users, run the following command:

cat /etc/passwd

The above command will list all the users in the environment. See the output:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
systemd-timesync:x:102:104:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false
uuidd:x:107:112::/run/uuidd:/usr/sbin/nologin
tcpdump:x:108:113::/nonexistent:/usr/sbin/nologin
sshd:x:109:65534::/run/sshd:/usr/sbin/nologin
landscape:x:110:115::/var/lib/landscape:/usr/sbin/nologin
pollinate:x:111:1::/var/cache/pollinate:/bin/false
sheeraz:x:1000:1000:,,,:/home/sheeraz:/bin/bash
delftstack:x:1001:1002::/home/delftstack:/bin/sh

Now, as we can see all the users in the system, we can change the user based on our requirements.

Use the su Command to Change the User in Bash

The su command, abbreviated as the switch user command, is used to change a user in Bash.

The syntax for this command is shown below:

su <option> <UserName>

Where the user name is the user you want to switch to. Now, for example, if we want to switch to the user delftstack, then we need to run the following command:

su - delftstack

The above command will ask for the user password, and with the correct password, it will switch the user. See the output:

Password:
delftstack@DESKTOP-Q5AQGI0:/mnt/c/Users/Sheeraz$

Use the sudo Command to Change the User in Bash

The sudo command is used to perform the task as administrator; it can also be used to change a user in Bash. The sudo command needs a password to run.

The syntax to change the user using the sudo command is:

sudo -u <UserName> -s

The above command will look for the UserName and switch to it. Let’s try an example:

sudo -u delftstack -s

This command will switch the user to delftstack. See the output:

delftstack@DESKTOP-Q5AQGI0:/mnt/c/Users/Sheeraz$

This command can also be used to launch a command as another user. This means if our user name is sheeraz and we want to run a command from delftstack, we can directly do it from the sheeraz user.

Let’s try an example in which we try to change the password for the user delftstack from the user sheeraz:

sudo -u delftstack passwd

The above command will change the password for the user delftstack using the current user sheeraz. See the output

Changing user password for user delftstack
Current password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Change the User to Root in Bash

It is often required to change to the root user to perform some operation. The su command in default can change the user to the root user; we can either run su or su - to switch to the root user in Bash.

Let’s try an example:

su -

Or:

su

Both of the above commands will switch the user from the current user to the root user. See the outputs:

Password:
root@DESKTOP-Q5AQGI0:/mnt/c/Users/Sheeraz$

Password:
root@DESKTOP-Q5AQGI0:/mnt/c/Users/Sheeraz$
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook