Linux and Unix tutorials for new and seasoned sysadmin
Linux and Unix tutorials for new and seasoned sysadmin
- Linux list all users command
- How to only List user names on Linux
- Say hello to getent command
- A Note About System and General Users
- Posted by: Vivek Gite
- Your support makes a big difference:
- 9 Answers 9
- To list
- To add
- To remove/delete
- To modify
- List all the users on Linux
- 1. Show users in Linux using less /etc/passwd
- 3. List Linux users with compgen
- A few tips about listing users in Linux
- List only the usernames
- Check if a username already exists in the system
- List all the connected users
Adblock detected
Linux list all users command
Type any one of the following command:
$ cat /etc/passwd
Sample outputs:
OR use pagers such as more/less command as follows to view /etc/passwd file:
$ more /etc/passwd
$ less /etc/passwd
Sample outputs:
Fig.01: List users using /etc/passwd
All fields are separated by a colon (:) symbol. Total seven fields exists. The first field is username. It is used when user logs in. It should be between 1 and 32 characters in length.
How to only List user names on Linux
To list only usernames type the following awk command:
$ awk -F’:’ ‘< print $1>‘ /etc/passwd
Sample outputs:
Another option is to use the cut command:
$ cut -d: -f1 /etc/passwd
Say hello to getent command
To get a list of all Linux users you type the following getent command:
$ getent passwd
$ getent passwd | grep tom
One can use the compgen command to list all users and other resources too:
$ compgen -u
A Note About System and General Users
Each user has numerical user ID called UID. It is defined in /etc/passwd file. The UID for each user is automatically selected using /etc/login.defs file when you use useradd command. To see current value, enter:
$ grep «^UID_MIN» /etc/login.defs
$ grep UID_MIN /etc/login.defs
Sample outputs:
1000 is minimum values for automatic uid selection in useradd command. In other words all normal system users must have UID >= 1000 and only those users are allowed to login into system if shell is bash/csh/tcsh/ksh etc as defined /etc/shells file. Type the following command to list all login users:
## get U /etc/login.defs) ## use awk to print if UID >= $U >= limit ) print $1>’ /etc/passwd
To see maximum values for automatic uid selection in useradd command, enter:
awk -F’:’ -v “min=$
Sample outputs:
In other words all normal system users must have UID >= 1000 (MIN) and U >
## get mini U /etc/login.defs) ## use awk to print if UID >= $MIN and U ‘< if ( $3 >= min && $3 , under GPL v2.0+ # ———————————————————————————— _l=»/etc/login.defs» _p=»/etc/passwd» ## get mini U $_l) ## use awk to print if UID >= $MIN and U ‘< if ( $3 >= min && $3 , under GPL v2.0+ # ———————————————————————————— _l=»/etc/login.defs» _p=»/etc/passwd» ## get mini U $_l) ## use awk to print if UID >= $MIN and U ‘< if ( $3 >= min && $3 = min && $3 Share on Facebook Twitter
Posted by: Vivek Gite
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.
Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:
I need a command to list all users in terminal. And how to add, delete, modify users from terminal.
That could help in administrating your accounts easily by terminal.
9 Answers 9
To list
To list all local users you can use:
To list all users capable of authenticating (in some way), including non-local, see this reply.
Some more useful user-management commands (also limited to local users):
To add
To add a new user you can use:
To remove/delete
To remove/delete a user, first you can use:
Then you may want to delete the home directory for the deleted user account :
Please use with caution the above command!
To modify
To modify the username of a user:
To change the password for a user:
To change the shell for a user:
To change the details for a user (for example real name):
To add a user to the sudo group:
And, of course, see also: man adduser , man useradd , man userdel . and so on.
Just press Ctrl + Alt + T on your keyboard to open Terminal. When it opens, run the command(s) below:
You can also use awk: awk
The easiest way to get this kind of information is getent — see manpage for the getent command . While that command gives the same output as cat /etc/passwd it is useful to remember because it will give you lists of several elements in the OS.
To get a list of all users you type (as users are listed in /etc/passwd )
To add a user newuser to the system you would type
to create a user that has all default settings applied.
Bonus: To add any user (for instance anyuser) to a group (for instance cdrom) type
You delete a user (for instance obsolete) with
If you want to delete his home directory/mails as well you type
will remove the user and all files owned by this user on the whole system.
You can use compgen GNU bash built-in too:
Will lists all users.
This should get, under most normal situations, all normal (non-system, not weird, etc) users:
- reading in from /etc/passwd
- using : as a delimiter
- if the third field (the User ID number) is larger than 1000 and not 65534, the first field (the username of the user) is printed.
This is because on many linux systems, usernames above 1000 are reserved for unprivileged (you could say normal) users. Some info on this here:
A user ID (UID) is a unique positive integer assigned by a Unix-like operating system to each user. Each user is identified to the system by its UID, and user names are generally used only as an interface for humans.
UIDs are stored, along with their corresponding user names and other user-specific information, in the /etc/passwd file.
The third field contains the UID, and the fourth field contains the group ID (GID), which by default is equal to the UID for all ordinary users.
In the Linux kernels 2.4 and above, UIDs are unsigned 32-bit integers that can represent values from zero to 4,294,967,296. However, it is advisable to use values only up to 65,534 in order to maintain compatibility with systems using older kernels or filesystems that can only accommodate 16-bit UIDs.
The UID of 0 has a special role: it is always the root account (i.e., the omnipotent administrative user). Although the user name can be changed on this account and additional accounts can be created with the same UID, neither action is wise from a security point of view.
The UID 65534 is commonly reserved for nobody, a user with no system privileges, as opposed to an ordinary (i.e., non-privileged) user. This UID is often used for individuals accessing the system remotely via FTP (file transfer protocol) or HTTP (hypertext transfer protocol).
UIDs 1 through 99 are traditionally reserved for special system users (sometimes called pseudo-users), such as wheel, daemon, lp, operator, news, mail, etc. These users are administrators who do not need total root powers, but who perform some administrative tasks and thus need more privileges than those given to ordinary users.
Some Linux distributions (i.e., versions) begin UIDs for non-privileged users at 100. Others, such as Red Hat, begin them at 500, and still others, such Debian, start them at 1000. Because of the differences among distributions, manual intervention can be necessary if multiple distributions are used in a network in an organization.
Also, it can be convenient to reserve a block of UIDs for local users, such as 1000 through 9999, and another block for remote users (i.e., users elsewhere on the network), such as 10000 to 65534. The important thing is to decide on a scheme and adhere to it.
Among the advantages of this practice of reserving blocks of numbers for particular types of users is that it makes it more convenient to search through system logs for suspicious user activity.
Contrary to popular belief, it is not necessary that each entry in the UID field be unique. However, non-unique UIDs can cause security problems, and thus UIDs should be kept unique across the entire organization. Likewise, recycling of UIDs from former users should be avoided for as long as possible.
Last Updated on September 10, 2019 By Helder 1 Comment
This tutorial shows you how to list users in Linux. You’ll also learn to list only the logged users.
Today different Operati ng Systems have the capability to use multiple users, each one with their settings and custom configurations to make things easier for administrators and operators to work in together on the same system.
Linux on the other hand is very strong on this matter as it allows multiple users to work at the same time on the system in an independent way. It can even allow a single user to open several sessions even from different locations in order to work on the system.
Here are some hints & tricks to handle users in Linux.
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.
1. 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:
The structure in the above output goes as:
- User name
- Encrypted password ( x represents password is stored)
- User >
Why 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.
Now if you want to distinguish the normal users from the system users, you can refer to the User >.
Generally , a normal user has UID greater or equal to 1000. This gives you a hint that the user with UID >=1000 is a normal user and users with UID 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.
The output would be like this:
You can use compgen command with -c option to list all the commands available to you. This is helpful when you are not the admin on a Linux system and don’t have sudo access.
A few tips about listing users in Linux
You just saw three ways to view users in Linux. Here are a few tips that would help you while dealing with the users listing.
List only the usernames
You already have the compgen command for that but you don’t have to remember it all the time.
If we would like to only get a list of the usernames in the system, you can use the awk command or the cut command to filter the output of the other two commands we saw earlier.
Any of these will give us a filtered list of users, showing only the very first column which is username:
Check if a username already exists in the system
This might be useful if you want to know if a particular username already exists in the system:
getent passwd | grep johndoe
List all the connected users
If you want to know what users are currently logged into your system, then you need to perform a simple ‘who’ on your command line and this will immediately list current usernames with an active session to your system
In this case, the listing will give you not only the list of usernames connected but also how they are connected, since when they are connected and from where they are connected.
The very first column will tell you what username is it.
The second column will give you what type of connection it is: if it’s represented with a “:X” where X is a number, it means it is using a Graphical User Interface (GUI) or Desktop session such as Gnome, XDE, etc; if it says “pts/X” where X is a number, it means it’s a connection made through SSH protocol (command line).
The third column will tell you since when this session has been connected to the server (date and time). The fourth and last column will give you the location from where it’s connected, if remote it will display the IP from where the connection is made if local (like the GUI) it will display “(:X)” where X is the number of the session in this case and will match the number in the second column for that row.
Wrapping up
As you can see, listing users in Linux is not difficult at all. It consists of simple commands which will output all the information for you, whatever you want to do or obtain of that information is something you need to filter depending on what you want to check on the system.
For example, if you want to list users in a group in Linux, you can do that as well. In a related topic, you may also read about changing users in Linux command line.
I hope you liked this tutorial. If you have any questions or suggestions, please let us know in the comments.
Liked the article? Please share it and help us grow 🙂