JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Changing Users in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

This article will explain how to change users in Bash.


Bash Change User

There are multiple ways to change users in Bash. We can use su command or sudo command to change users directly or use methods to switch to root user.

To change users in Bash, first, we need to know the name of the user in the environment. To list the username, 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
jiyik:x:1001:1002::/home/jiyik:/bin/sh

Now, we can see all the users in the system and we can change the user as needed.


Changing Users in Bash Using the su Command

suThe command, abbreviated as switch user command, is used to change users in Bash.

The syntax for this command is as follows:

su <option> <UserName>

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

su - jiyik

The above command will ask for the user password. Enter the correct password to switch users. View the output:

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

Changing Users in Bash Using the sudo Command

sudoThe command is used to perform tasks as an administrator; it can also be used to change users in Bash. sudoThe command requires a password to run.

sudoThe syntax for changing a user using the command is:

sudo -u <UserName> -s

The command above will look up the username and switch to it. Let's try an example:

sudo -u jiyik -s

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

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

This command can also be used to launch commands as other users. This means if our username is sheeraz and we want to run a command from jiyik, we can do it directly from the sheeraz user.

Let's try an example where we try to change the password of user jiyik from user sheeraz:

sudo -u jiyik passwd

The above command will change the password of user jiyik with the current user sheeraz. See the output

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

Change User to Root in Bash

Often you need to switch to the root user to perform some operations. The default su command can change the user to the root user; we can run su or su - to switch to the root user in Bash.

Let's try an example:

su -

or:

su

Both commands will switch the user from the current user to the root user. See the output:

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

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

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial