SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 410244
Accepted
nux
nux
Asked: 2014-01-25 11:20:51 +0800 CST2014-01-25 11:20:51 +0800 CST 2014-01-25 11:20:51 +0800 CST

Is there a command to list all users? Also to add, delete, modify users, in the terminal?

  • 772

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.

command-line
  • 9 9 Answers
  • 3471510 Views

9 Answers

  • Voted
  1. Best Answer
    Radu Rădeanu
    2014-01-25T12:23:12+08:002014-01-25T12:23:12+08:00

    To list

    To list all local users you can use:

    cut -d: -f1 /etc/passwd
    

    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

    or:

    sudo useradd new_username

    See also: What is the difference between adduser and useradd?

    To remove/delete

    To remove/delete a user, first you can use:

    sudo userdel username

    Then you may want to delete the home directory for the deleted user account :

    sudo rm -r /home/username

    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:

    sudo passwd username

    To change the shell for a user:

    sudo chsh username

    To change the details for a user (for example real name):

    sudo chfn username

    To add a user to the sudo group:

    adduser username sudo

    or

    usermod -aG sudo username

    And, of course, see also: man adduser, man useradd, man userdel... and so on.

    • 1347
  2. Mitch
    2014-01-25T11:28:53+08:002014-01-25T11:28:53+08:00

    Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:

    cat /etc/passwd
    

    OR

    less /etc/passwd
    more /etc/passwd
    

    You can also use awk:awk

    awk -F':' '{ print $1}' /etc/passwd
    
    • 114
  3. guntbert
    2014-02-03T10:50:08+08:002014-02-03T10:50:08+08:00

    The easiest way to get this kind of information is getent - see manpage for the getent command Manpage icon. 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)

    getent passwd
    

    To add a user newuser to the system you would type

    sudo adduser newuser
    

    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

    sudo deluser obsolete
    

    If you want to delete his home directory/mails as well you type

    sudo deluser --remove-home obsolete
    

    And

    sudo deluser --remove-all-files obsolete
    

    will remove the user and all files owned by this user on the whole system.

    • 78
  4. Ravexina
    2017-06-15T04:51:15+08:002017-06-15T04:51:15+08:00

    You can use compgen GNU bash built-in too:

    compgen -u
    

    Will lists all users.

    • 53
  5. Wilf
    2015-06-09T12:03:28+08:002015-06-09T12:03:28+08:00

    This should get, under most normal situations, all normal (non-system, not weird, etc) users:

    awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
    

    This works by:

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

    • 33
  6. Donovan Vesters
    2014-09-26T07:47:50+08:002014-09-26T07:47:50+08:00

    list of all users who can login (no system users like: bin,deamon,mail,sys, etc.)

    awk -F':' '$2 ~ "\$" {print $1}' /etc/shadow
    

    add new user

    sudo adduser new_username
    

    or

    sudo useradd new_username
    

    delete/remove username

    sudo userdel username
    

    If you want to delete the home directory (default the directory /home/username)

    sudo deluser --remove-home username
    

    or

    sudo rm -r /path/to/user_home_dir
    

    If you want to delete all files from the system from this user (not only is the home diretory)

    sudo deluser --remove-all-files
    
    • 19
  7. user239243
    2014-01-25T11:38:26+08:002014-01-25T11:38:26+08:00

    Ok here is a trick that will help you sort this. The terminal has auto completion if you type user and hit Tab key twice it will list all the commands that exist with user as the first 4 chars.

    user (tab tab)
    

    gives me as possible options useradd userdel usermod users users-admin
    if you want to know more about a command google it or type man man useradd gives useradd - create a new user or update default new user information ... ...

    to list users you should go with what Mitch said.

    Hope that helps I love tab completion in bash saves me from remembering things.

    • 8
  8. anvesh
    2014-12-19T14:53:44+08:002014-12-19T14:53:44+08:00

    To find out the users which have home-directories in the /home-folder on the machine, run the following commands

    cd /home
    ls 
    

    You can then see the users who have authorization to log into the server. If we want to look into the files of any users, you must be the root user.

    • 7
  9. WinEunuuchs2Unix
    2019-11-14T17:33:17+08:002019-11-14T17:33:17+08:00

    The first answer recommends:

    cut -d: -f1 /etc/passwd
    

    But using this and counting number of users you get:

    $ cut -d: -f1 /etc/passwd | wc -l
    46
    

    46 users for a laptop computer are a lot!. So use this instead:

    $ cat /etc/passwd | grep -vE '(/bin/false|/sbin/nologin|/bin/sync)' | cut -d: -f1
    root
    rick
    guest-atkb2q
    guest-u4sf2i
    guest-rmlbtg
    guest-mz53vp
    

    To remove guest accounts (who don't have saved files anyway) use:

    $ cat /etc/passwd | grep -vE '(/bin/false|/sbin/nologin|/bin/sync|guest-)' | cut -d: -f1
    root
    rick
    

    Some sample users removed from the listing are:

    systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
    gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
    sync:x:4:65534:sync:/bin:/bin/sync
    sync:x:4:65534:sync:/bin:/bin/sync
    guest-atkb2q:x:999:999:Guest:/tmp/guest-atkb2q:/bin/bash
    

    It turns out most of the users on a single user system are actually programs that have set themselves up as users.

    • 1

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to install Google Chrome

    • 8 Answers
  • Marko Smith

    Is there a command to list all users? Also to add, delete, modify users, in the terminal?

    • 9 Answers
  • Marko Smith

    How to delete a non-empty directory in Terminal?

    • 4 Answers
  • Marko Smith

    How to unzip a zip file from the Terminal?

    • 9 Answers
  • Marko Smith

    How can I copy the contents of a folder to another folder in a different directory using terminal?

    • 8 Answers
  • Marko Smith

    How do I install a .deb file via the command line?

    • 11 Answers
  • Marko Smith

    How do I run .sh scripts?

    • 16 Answers
  • Marko Smith

    How do I install a .tar.gz (or .tar.bz2) file?

    • 14 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Martin Hope
    Flimm How can I use docker without sudo? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    led-Zepp How do I save terminal output to a file? 2014-02-15 11:49:07 +0800 CST
  • Martin Hope
    ubuntu-nerd How to unzip a zip file from the Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    pandisvezia How can I copy the contents of a folder to another folder in a different directory using terminal? 2011-12-11 17:19:37 +0800 CST
  • Martin Hope
    TheXed How do I install a .deb file via the command line? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve