List current users in linux

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 which changes user details (for example real name). I tried to add this as a comment, but I got error telling me that I must have +50 reputation to do so.

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.

Manpage icon

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.

Читайте также:  Установка rpm пакетов alt linux

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 get arch in linux

12 Ways to Find User Account Info and Login Details in Linux

This article will show you useful ways to find information about users on a Linux system. Here we’ll describe commands to get a user’s account details, show login details as well what users are doing on the system.

If you want to add or create users in Linux, use the useradd command, and to modify or change any attributes of an already created user account, use the usermod command via the command line.

You might also like:

We’ll start by looking at 12 useful commands to find a user’s account information, then proceed to explain commands to view login details in the Linux system.

1. id Command – Show User and Group IDs

The id is a simple command line utility for displaying a real and effective user and group IDs identity information for the current user or specified user.

View User and Group ID

2. groups Command – View User Group Memberships

The groups command is used to display the group memberships for a user. It lists all the groups that a user belongs to, including both primary and supplementary groups.

Check User Groups in Linux

3. finger Command – Show User Information

The finger command is used to search for information about a user on Linux, which includes detailed information about a specific user or a list of users, including their login name, real name, terminal, idle time, login time, and other relevant details.

The finger command doesn’t come pre-installed on many Linux distributions, you need to install it using your default package manager as shown.

$ sudo apt install finger [On Debian, Ubuntu and Mint] $ sudo yum install finger [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/finger [On Gentoo Linux] $ sudo apk add finger [On Alpine Linux] $ sudo pacman -S finger [On Arch Linux] $ sudo zypper install finger [On OpenSUSE]

It shows a user’s real name; home directory; shell; login: name, time; and so much more as shown below.

Check User Info in Linux

4. getent Command – Fetch User Info from System Database

The getent command is used to retrieve information from various databases, including the system user and group databases. It can be used to retrieve information about users, groups, hosts, networks, protocols, and other system entities that are stored in database files like /etc/passwd, /etc/group, /etc/hosts, etc.

Читайте также:  Чем прочитать дамп linux

To get a user’s account details, use the passwd database and the username as follows.

Fetch User Info in Linux

5. grep Command – Search for Patterns or Specific Text in Files

The grep command is a powerful command used to search for patterns or specific text in files. It allows you to filter and extract lines from text based on matching patterns. The name “grep” stands for “Global Regular Expression Print“.

You might also like:

You can use grep to find information about a specific user from the system accounts file: /etc/passwd as shown below.

Grep - Fetch User Info in Linux

6. lslogins Command – Display User Information in Linux

The lslogins command shows information about known users in the system, which typically includes details such as the username, UID (User ID), GID (Group ID), home directory, shell, last login time, and more, depending on the options used and the system configuration.

$ lslogins -u tecmint $ lslogins -u

Display User Info in Linux

7. users Command – List Current Logged-In Users on Linux

The users command is used to display the list of currently logged-in users on the Linux system.

Show Current Logged in Users in Linux

8. who Command – Show Information Of Currently Logged-In Users

The who command is used to display users who are logged on to the system, including the username, terminal, login time, and remote host from which the user is logged in.

Show Current Logged in Users

9. w Command – Show Currently Logged-In User Activity

The w command shows a summary of the currently logged-in users and their activity, which displays the login session, including the username, terminal, login time, idle time, JCPU (total CPU time used by all processes), PCPU (CPU time used by the current process), and the command or process running on the terminal.

Show Currently Logged-In User Activity

10. last Command – Show Most Recent Login Session

The last command displays a list of the most recent login sessions, which includes information about the users who have logged in, their login times, and the terminals or remote hosts they used for login.

Show Most Recent Login Sessions

To show all the users who were present at a specified time, use the -p option as follows.

List Most Recent User Logins

11. lastb Command – Show Failed Login Attempts

The lastb command is used to display a list of the last failed login attempts on the system. It reads from the system log file that records failed login attempts, typically stored in /var/log/btmp.

Show Failed Login Attempts in Linux

12. lastlog Command – List User Login Information

lastlog command is used to find the details of the most recent login information for all users or a specific user on the system, which provides details about the last login time and location for each user.

$ lastlog OR $ lastlog -u tecmint

List User Login Information

That’s it! If you know any other command-line trick or command to view user account details do share with us.

You might also like:

In this article, we’ve explained various ways to find information about users and login details on a Linux system. You can ask any questions or share your thoughts via the feedback form below.

Источник

Оцените статью
Adblock
detector