- Is there a command to list all users? Also to add, delete, modify users, in the terminal?
- 10 Answers 10
- To list
- To add
- To remove/delete
- To modify
- How to List Users in Linux
- Listing Users in Linux
- List Users with cat Command
- List Users with Terminal Pagers less and more
- List Users with awk Command
- List Users with getent Command
- Listing Normal and System users in Linux
- How can I display all users and groups with a command?
- 2 Answers 2
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
Is there a command to list all users? Also to add, delete, modify users, in the terminal?
I need a command to list all users as well as commands to add, delete and modify users from terminal — any commands that could help in administrating user accounts easily by terminal.
10 Answers 10
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:
sudo adduser new_username
sudo useradd new_username
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:
usermod -l new_username old_username
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.
Radu forgot to mention sudo chfn
I think that it should be underlined that the correct answer to the linked question is askubuntu.com/a/381646/16395 — otherwise you have to take into account the GID/UID Ubuntu policies by hand. The accepted answer is not so clear.
sudo userdel DOMAIN\\johndoe gives me the error: «userdel: cannot remove entry ‘DOMAIN\johndoe’ from /etc/passwd — I looked in /etc/passwd and they’re not even in there, likely because it’s a «domain» account?
@00fruX Yeah. If you’re using a centralised user database you’re going to need to deal with it directly.
Just press Ctrl + Alt + T on your keyboard to open Terminal. When it opens, run the command(s) below:
less /etc/passwd more /etc/passwd
@nux A bit late to the party, but from command line use adduser instead, useradd should be limited to scripts where the author really really knows what he is doing.
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
sudo adduser anyuser cdrom
You delete a user (for instance obsolete) with
If you want to delete his home directory/mails as well you type
sudo deluser --remove-home obsolete
sudo deluser --remove-all-files obsolete
will remove the user and all files owned by this user on the whole system.
It’s useful to remember that getent doesn’t just print the output of users in /etc/passwd but all users in all configured userdb backends on a given system, whether it’s /etc/passwd or LDAP, etc.
@MarcinKaminski is right, it also prints users setup in SSO systems who have access to the server. this answer is the the best one, with getent passwd being the right command
This should get, under most normal situations, all normal (non-system, not weird, etc) users:
awk -F'[/:]' '= 1000 && $3 != 65534) print $1>' /etc/passwd
- 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.
How to List Users in Linux
User management is a critical Linux system administration task. In large organizations, having insight into who has access to the system is crucial to correctly add users, remove users, and assign new user privileges.
This tutorial will show you how to list users on a Linux-based system. The guide provides four listing methods and explains essential concepts related to user administration.
Listing Users in Linux
Linux stores information about local users in the /etc/passwd file. Each line in the file contains information about a single user, including their username, user ID number (UID), home directory, and the login shell.
The following sections present multiple ways to access the data in /etc/passwd and list users on Linux distributions.
The commands used in the tutorial are:
Note: To display a list of the logged-on users and the information such as boot time, processes, hostnames, and more, use the who command.
List Users with cat Command
The cat command provides a straightforward way to list the contents of the /etc/passwd file.
The system outputs the entire file with all the users on the system.
To view the number of users only, pipe the output of the previous command to the wc command and make it count the number of lines:
The number of lines in /etc/passwd corresponds to the total number of users.
List Users with Terminal Pagers less and more
On systems with many users, it is useful to limit the /etc/passwd file output displayed at once. Use a terminal pager command, such as less or more , to browse through the file content line by line or page by page.
To open /etc/passwd using less , enter:
The first page of the file appears in the output. The list stops when it reaches the end of the terminal screen. Use the keyboard to navigate through the file.
Use more to get a similar result. This command is older and has a more limited set of functionalities:
List Users with awk Command
Use the awk command to list the usernames only, without additional information about each user. Since the data fields in /etc/passwd are separated by a colon symbol, the following syntax tells awk to output only the first field in each line:
Combine awk and less for a page-by-page view of the results.
List Users with getent Command
The getent command searches and displays system database entries. The searchable databases are listed in the /etc/nsswitch.conf file. By default, the file includes the passwd database.
List the entire contents of the passwd database by typing:
The output is the same as the output of the cat command.
However, you can use getent to look up specific users. To do so, use the following syntax:
If the user exists on the system, the command shows the related passwd entry line.
Listing Normal and System users in Linux
Linux-based systems have two types of users — system and normal users.
- System users are entities created by the system to run non-interactive processes, i.e., the processes that run in the background and do not require human interaction. The most important system user is root, which possesses administrative privileges.
- Normal users are human users created by root or another user with root privileges. Each normal user has a login shell and a home directory to store their files.
Both system and normal users in Linux have a unique user ID (UID) to identify them. System users have UIDs in the range from 0 (root user) to 999. Normal users typically receive UIDs from 1000 onwards, with each newly created user receiving the next smallest unused UID.
To check the UID range for normal users, use the grep command to search for the information stored in /etc/login.defs :
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
The output in this example shows that the smallest UID a normal user can receive is 1000, and the largest is 60000.
Use getent to search the passwd database by UID:
The output shows the user entry related to the UID.
Use UIDs in combination with getent to search for users in a range:
The command now lists all the users within the specified UID range.
This guide showed you how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution.
How can I display all users and groups with a command?
users and groups commands display users currently logged in, and groups a user belongs to respectively.
How to display a list of all users and all groups by command-line?
2 Answers 2
You can display with the help of compgen builtin command as follows:
- To display all users run following command:
However you can also display all users by cut -d «:» -f 1 /etc/passwd .
Nice! it might be preferable to use getent passwd / getent group instead of cat’ing the local files ( getent should work for non-local accounts as well)
Well, on my ubuntu, I have some files created by docker mount with 999:999 as user:group , but unfortunately none of the above commands prints them.
Here we are going to use getent for the detailed the info
We can list the user with the following command:
We can list the group as follows:
To fetch detail a specific user
Replace the lalit with your user name. Lalit will not be in every system 🙂
You can read the more into about getent here
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.