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

Hosting Docker Internal in Linux

Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM

Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t

Setting the working directory in Docker

Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM

If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR

How to get IP address in CentOS

Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM

This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for

Updating YUM in Linux

Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM

This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux

Installing Deb Files in Linux

Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM

In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy

lsof Command in Linux

Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM

In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U

ps aux command in Linux

Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM

If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial