Skip to main content

It is really simple to switch users in Ubuntu or any other Linux distribution using the terminal, iTerm2 or your go to shell program.

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

su another_username

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

 

List all the users on Linux

Let’s say you want to create a sudo user in Linux. Probably, the very first thing to know is how to know what users are in my system. There are several ways you can obtain the list of users in Linux.

Show users in Linux using
less /etc/passwd

This command allows sysops to list the the users that are locally stored in the system. It will give the listing in structured way as:

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
.... /

The structure in the above output goes as:

User name
Encrypted password (x represents password is stored)
User ID number (UID)
User’s group ID number (GID)
Full name
User’s home directory
User’s Login shell (default is bash shell)

The output list will be longer than what is listed above.  So many users!  Which ones are real?  The list shows a lot more users than you expected because it lists all the system users too.  To distinguish the normal users from the system users, you can refer to the User identifier (UID) number.  Generally, users with UID >=1000 is a normal user and users with UID <1000 are system users.

You’ll also notice that some of the users have ‘nologin’ at the end of their line. This means that these users cannot login to the system. These users are also referred as pseudo-users.

 

2. View users using getent passwd
getent passwd

This command will deliver a similar output as “less /etc/passwd” however, this one actually queries the GNU Name Service Switch functionality configuration file (located at /etc/nsswitch.conf).

This conf includes passwd, so that’s why it will display very similar but if you use LDAP for authentication it will include that as well.

 

3. List Linux users with compgen

If you just want to list all the usernames without any additional information, you can use the compgen command with -u option.

compgen -u

The output would be like this:

root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats

Again not a compete list of the output.  More to give you an idea.

Related articles

Andrew Fletcher01 May 2024
Common commands used in Ubuntu - in progress
A growing list of commands I've used and what they do in no specific ordersudo snap install bw ps aux | grep java whoami ip addr show uptime sudo apt update &amp;&amp; sudo apt upgrade -y cat /etc/os-release sudo apt-get install needrestart sudo reboot sudo needrestart sudo ckan sysadmin...