UNIX User Management
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
Linux
sudo adduser <username>
FreeBSD
sudo adduser
Creating a Group
Linux
sudo groupadd <group>
FreeBSD
sudo pw groupadd <group>
Adding a User to a Group
Linux
sudo usermod -a -G <group> <user>
FreeBSD
sudo pw groupmod <group> -m <user>
Adding a User to Multiple Groups
Linux
sudo usermod -a -G <group1>,<group2>,<group3> <user>
Changing the User’s Primary Group
Linux
sudo usermod -g <group> <user>
FreeBSD
sudo pw usermod -n <user> -g <group>
Deleting a User
Linux
sudo userdel -r <user>
FreeBSD
sudo rmuser <user>
Deleting a Group
Linux
sudo groupdel <group>
FreeBSD
sudo pw groupdel <group>
Details
- adduser
- 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 hasuseradd
. Unless you’re writting scripts, you probably never want this. It’s not interactive. While runningsudo 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-G
. - groupmod
- 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. - userdel
- 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
rmuser
does a lot of additional clean up, like killing running process, deleting email, and removing cron jobs.- groupdel
- 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.
Comments