Skip to content

Linux Unix add, edit and remove users and groups

Michael Hulse edited this page Sep 26, 2018 · 7 revisions

Users

useradd is native binary compiled with the system. But, adduser is a perl script which uses useradd binary in back-end.

adduser is more user friendly and interactive than its back-end useradd. There's no difference in features provided.

adduser isn't available on all distros (eg. suse-based), and on some others it's just a symlink to useradd (redhat-based).

Note: See groups section below to create user and add to group in one fell swoop.

# List local users:
$ cut -d: -f1 /etc/passwd
# Add a new user (use `-m` option (no args) to create the user’s home directory automatically.
# However, it is not necessary to use it on Red Hat and other systems on which such directory is created automatically:
$ sudo adduser myuser
# Remove a user:
$ sudo userdel myuser && rm -r /home/myuser
# Set password:
$ sudo passwd myuser
Changing password for user myuser.
New password: xxxx
Retype new password: xxxx

Groups

# View All Groups on the System
$ getent group

# Create new group:
$ sudo groupadd <new-group-name>

# View groups for user:
$ groups <user>

# Show your user’s primary group:
$ id -gn

# No user will default to your user:
$ groups

# Create a new user and assign a group in one command:
$ useradd -G <group> <user>

# Assign/change a user’s primary group:
$ usermod -g <primary-group-name> <user>

# Add a user to multiple secondary groups:
$ usermod -a -G <group1>,<group2>,<group3> <user>

# Add user to existing secondary group (`-a` keeps already existing secondary groups intact, otherwise they would be removed):
$ sudo usermod -a -G <existing-group> <user>

# View the numerical IDs associated with each group:
$ id

# Same as above:
$ id -a

# View the numerical IDs associated with each group:
$ id <user>

Remember: if changing/adding groups for your user, you will need to log out and back in again.

Clone this wiki locally