From time to time (more so if you’re a homelabber or sysadmin) you’ll have to perform user management on your system. This can be accomplished by using the useradd
and userdel
commands. The useradd
command allows admins to create new users, while userdel
(as you can imagine) allows us to delete existing users. Let’s look a few examples of this below:
Using USERADD to Create Users
The useradd
command is used to create new users or update user information such as setting a user’s home directory, password, and other settings. Let’s create a new user:
sudo useradd linuxman
useradd
is to also pass the
-m
option which will automatically create the new user’s home too and set the permissions on that dir correctly.
sudo useradd -m linuxman
useradd
but it also wack as the password can be seen by anyone listing the processes. Instead we will use passwd
.
sudo passwd linuxman
New password:
Retype new password:
passwd: password updated successfully
passwd
will prompt you to enter a password for the given user, note that the terminal will not show your password nor the amount of characters as a security measure, thus double check you’ve typed your password correctly.Adding users to groups
To add your user to existing groups on the system we would use the -G
option and specify what group you want to add the user to during user creation. Here we are creating a user and adding them to the group “wheel” (or sudo
group).
sudo useradd -m -G wheel linuxman
Always use a secure password for your new users!
usermod
, but that is beyond the scope of this post.Changing the defaults
All the above examples are good for single or infrequent uses, but if you find yourself constantly having to make new users or some other unique circumstance, we can edit this file /etc/login.defs
. This file will have a ton of options that modify the default useradd
behavior.
#/etc/login.defs
...
# If useradd(8) should create home directories for users by default (non
# system users only).
# This option is overridden with the -M or -m flags on the useradd(8)
# command-line.
#
#CREATE_HOME yes
...
The option we are looking for is called #CREATE_HOME
which will be commented out, simply remove the # and any user made after this edit will have their home directory created without having to pass -m
.
Using USERDEL to Delete Users
userdel
command along with the username you want to remove:
sudo userdel linuxman
Note that unless you specify -r
, then that users’ home directory won’t be removed. The complete command will look like this:
sudo userdel linuxman -r
Conclusion
useradd
and userdel
to your terminal know-how will make administrating any system a breeze as you’ll be able to add/remove users without much fuss. As always It is important to remember to proceed with caution when it comes to user management, as you can very easily wreak havoc on a system. Thanks for reading!