Working with Users & Groups

Hello friends,

in this article I am showing you how to work with users and groups on an Ubuntu Server on the command line interface. We are using the following commands: adduser, usermod, deluser, id, passwd, groupadd & groupdel. I expect that you have a working Ubuntu Server installed but if you need help with that look at simple Ubuntu Server installation.

User & Group management

In the first part of this article we are going over the user creation, removal and modification and in the second part we go over the group creation & removal.

Users

There is a naming convention for usernames which should be followed if you like to prevent issues later down the road.

  • Usernames should be lowercase all the way
  • Usernames should not start with numbers
  • Usernames should not have special characters with the exception of “” or “_
  • Usernames should be ideally no longer than 8 characters

Adding User Account – adduser

On Ubuntu Server one can use the adduser command which will guide you through a user account setup. Run the following command:

sudo addusers new-user1 (replace new-user1 with a desired username).

adduser command
adduser command

As you can see it is fairly simple on an Ubuntu Server to add a new user account. Go ahead and try this for yourself.

Adding user to group – usermod

If you like to add this newly created user to a group lets say you like to grant sudo permission for that user you would need to add the user to the sudo users group.

sudo usermod -aG sudo new-user1

Explanation (sudo usermod -aG <group> <username>:

  • sudo : command to elevate to root user permissions
  • usermod : command to modify an existing user account
  • -aG : Those are two command line switches, “a” for append and “G” for specifying a Groupname with other words append to Group
  • <group> : the group name of the group you like to add the user to
  • <username> : the username you like to modify

Changing user password – passwd

If there is a need for a user to change their password run the following command:

passwd

it should something like the following:

passwd command

if you need to change the password for another user run the following command:

sudo passwd new-user2

Deleting user account – deluser

In order to delete user accounts in Linux one has to use the deluser command and I am showing you two variation of it. The first variation only deletes the user account with the following command:

sudo deluser username

and this second variation deletes the user account as well as its home directory. Execute the following command:

sudo delsuser –remove-home username

Locking & unlocking user account

In Linux one is able to lock user accounts as well with the following command:

sudo usermod -L new-user1

now lets say one mistakenly locked an account and need to unlock it. Run the following command to unlock a user account:

sudo usermod -U new-user1

Displaying user information – id

Linux has a command to display user information it prints the user and group information for the specified username or if no username is given it prints the information of the current user. Run the command id by itself and the run it with a username.

id

or

id new-user1

you should be seeing something like the following:

id command
id command

Groups

Adding a group – groupadd

In order to create a new group run the following command:

sudo groupadd groupname

by default Linux is using the next available group ID

groupadd command
groupadd command

Deleting a group – groupdel

If there is need to delete a group you can do so by executing the following command:

sudo groupdel groupname

Conclusion

In this article we went over adding, removing and modifying user accounts, displaying account information and how to create and delete groups. If you like to read more about user management on Ubuntu Server read here. I hope you found this article informational and if you like for me to add a specific article please let me know in the comments.

Ciao 😉