This is one of the blog posts to set something in my mind that I’m
always looking up. Linux has lovely tools for adding and managing
users, but I can never remember them. I’m old skool and BSD-centric,
so I tend to just use vipw, but it’s better to use the tools. So,
with no further ado:
Creating a User
sudo adduser <username>
Creating a Group
sudo groupadd <group>
sudo pw groupadd <group>
Adding a User to a Group
sudo usermod -a -G <group> <user>
sudo pw groupmod <group> -m <user>
Adding a User to Multiple Groups
sudo usermod -a -G <group1>,<group2>,<group3> <user>
Changing the User’s Primary Group
sudo usermod -g <group> <user>
sudo pw usermod -n <user> -g <group>
Deleting a User
sudo userdel -r <user>
sudo rmuser <user>
Deleting a Group
sudo groupdel <group>
sudo pw groupdel <group>
With adduser on Linux you just supply a username, you’ll get prompted for
everything else, including the username on BSD systems.
Confusingly, Linux also has useradd. Unless you’re writting scripts,
you probably never want this. It’s not interactive. While running
sudo useradd <username> will add the account to the password file,
it won’t setup the home directory or copy in init files unless you
explicitly tell it to.
usermod for groups
The -a is for add or append. If it’s left off the
listed groups will replace the existing ones. This only works with
The BSD only pw groupmod simple adds the user to the group. FreeBSD
doesn’t have a simple way to add a user to multiple groups in one
command. However, the -m option can take multiple, comma
separated list of users.
The -r removes the user’s home directory, without it, the user is
removed from the password file, but the it’s files are left intact.
rmuser does a lot of additional clean up, like killing running
process, deleting email, and removing cron jobs.
The Linux groupdel won’t remove a group if it’s the primary group of any
user. You must change the user’s group or delete the user
first. FreeBSD doesn’t seem to have this feature.
Now maybe I’ll remember next time I need to do user management.