So you’re managing your personal Ubuntu machine or administering a server, understanding how to handle users and groups can really make your life easier.
🚀 Creating Users: System and Normal
For the Regular Users Creating a normal user is like inviting a new friend to your Ubuntu party. Simply pop open your terminal and type:
sudo adduser tonystark
This command conjures up a new user named tonystark, complete with a home directory and an interactive process to set up the account.
The lower-level equivalent is explicit about creating the home and shell:
sudo useradd --create-home --shell /bin/bash tonystark
sudo passwd tonystark
For the System User They’re not for daily use but for running services and applications. To create one, do a little terminal magic with:
sudo adduser --system my-awesome-service-user
This creates a system user without a home directory by default.
🧙♂️ Conjuring Groups and Modifying Memberships
Creating a Group: To start a new group, simply:
sudo addgroup codingwizards
Now codingwizards is ready for members!
Adding a User to a Group: Got a user that needs to be in a group? No problem!
sudo adduser newfriend codingwizards
Voilà! newfriend is now a proud member of the codingwizards.
Forming a Group After the Fact: Did you forget to add your user to a group during their creation? Fear not!
sudo usermod -aG codingwizards newfriend
The -aG option appends the user to the specified group without removing them from their current groups.
🚫 Going NoLogin and Setting Up Shop (Home Directories)
Silencing the Login: Want a user that can’t login? It’s great for system users running services:
sudo usermod -s /usr/sbin/nologin ghostlyassistant
Now, ghostlyassistant can’t log in, which is perfect for a background service user.
Creating a Home After the Fact: Forgot to give your system user a place to live? Let’s fix that:
sudo install -d -o ghostlyassistant -g ghostlyassistant -m 750 /home/ghostlyassistant
sudo usermod -d /home/ghostlyassistant ghostlyassistant
And just like that, ghostlyassistant has a cozy home in your system.
🆔 Assigning Specific IDs
Sometimes, you need to get specific about user and group IDs for various reasons (like matching permissions across systems).
User ID (UID):
sudo useradd --create-home -u 1001 specialuser
Group ID (GID):
sudo groupadd -g 2001 specialgroup
And there you have it, specialuser and specialgroup with custom IDs.
🌈 Removing Users and Groups
Sometimes, parties end, and it’s time to say goodbye:
To remove a user:
sudo deluser oldfriend
This leaves the home directory by default. When I have checked the path and intend to delete it too, I use sudo deluser --remove-home oldfriend.
To dissolve a group:
sudo delgroup oldclub
Just like that, oldfriend and oldclub are part of your system’s history.
I verify the resulting account and groups:
getent passwd tonystark
getent group codingwizards
id tonystark
These are the Debian/Ubuntu convenience tools documented by adduser; useradd is the lower-level alternative.
Buy Me a Coffee