Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 89 down vote accepted

To list all users you can use:

cut -d: -f1 /etc/passwd

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

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

share|improve this answer
3  
thanks man for your perfect explained answers hope it can help others also –  nux Jan 24 at 21:24
5  
Radu forgot to mention sudo chfn <username> 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. –  Mikaela Jan 29 at 7:26
    
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. –  Rmano May 21 at 18:02
    
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 Aug 7 at 19:44
    
@00fruX Yeah... If you're using a centralised user database you're going to need to deal with it directly. –  Oli Aug 8 at 8:32

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
share|improve this answer
    
how to add users by command ? –  nux Jan 24 at 19:31
    
You can use useradd command. –  Mitch Jan 24 at 19:32
    
thank you man for your answer –  nux Jan 24 at 22:05

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.

share|improve this answer
1  
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. –  Marcin Kaminski Sep 25 at 16:34

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.

share|improve this answer
    
thank u , but this is not my answer –  nux Jan 25 at 1:05
    
No problem. How the info was useful. –  user239243 Jan 25 at 1:06

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
share|improve this answer
    
Maybe you should explain the difference between adduser and useradd. An also add the sudo-prefix to the first command. The password shadow file can only be read as root. –  the_Seppi Sep 25 at 20:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.