In this guide I will be showing you how to manage local user accounts ranging from creation to modification including things such as adding to sudoers list and changing passwords. You will need root permissions so for convenience sake we will just switch to root using “sudo su”.
Creating A User Account
You can create a user account by using the useradd command. There are plenty of options you can specify while creating a user, for a full list you can simply run useradd by itself. Please note that your new user will be unable to login until you set a password using the second command.
In the below example I will create a standard local user named mikey.
useradd -c "mikey" -d "/home/mikey" -m -s "/bin/bash" mikey # Create the user passwd mikey # Set a password for the user
You can verify the user was created successfully by running the two following commands.
grep mikey /etc/passwd ls -l /home | grep mikey
Modifying A User Account
You can modify a user account after you created it by using the usermod command. You can change a variety of things with this command ranging from their default shell to their “full name”. For a full list of options simply run the command usermod by itself. In the example below I will change the users actual name (not login name) to Michael.
usermod mikey -c "Michael" # Apply changes grep mikey /etc/passwd # Verify changes
Modifying A User Password
To change a local user’s password all you will need to do is run the passwd command as root. Below is an example of how to change the password for the account I created during this tutorial.
passwd mikey
Disabling A User Account
To disable a user account without removing it or in other words prevent them from logging in, all you need to do is run the passwd command with -l. What it does is basically add a ! in-front of the user’s password hash in the shadow file. To reverse it change -l to -u.
passwd mikey -l # Lock this account passwd mikey -u # Unlock this account
Granting/Revoking A User Sudo Permissions
To add a user to the sudoers list which will give them permission to run commands under root privileges as-well as login as root (sudo su), you simply have to add them to the sudo user group with the following command. If they are currently logged in when you run this, they will need to re-login. This is also how you add or remove a user from any other group that you may want them to be part of.
adduser mikey sudo # Add user to sudo group deluser mikey sudo # Remove user from sudo group
Removing A User Account
If you want to remove a user then the userdel command will do this for you. You have the option of removing the user but keeping their home directory and the files within it or removing them as-well (you may get a similar error to the one shown but that is ok). Below is an example of how to do both options.
userdel -r mikey # Remove user and home directory userdel mikey # Remove user but keep home directory
I hope this tutorial has helped you learn how to work with user accounts. Please don’t forget to comment, or share!