Ubuntu - manage User & groups security with low level commands
Published on: 22nd Apr 2014
Updated on: 16th Jul 2025
Overview
Here's the commands that you need to do the basic user and group management.
Managing users and groups
-
To add user, run the following command and it will create user record and an user directory in
/home
,sudo useradd ftpuser
12.Jan.2023 - alternatively, you may use the high level command call
adduser
which creates the user home directory, ask for password and the user contact information.sudo adduser ftpuser
-
To set password for the given user (in this case, 'ftpuser'), run the following command and then key in the password,
sudo passwd ftpuser
-
To view the user who is connected to server, execute one of the following command,
who w users
-
To view the user groups of the current session in the OS,
groups
-
To view all groups in the server,
sudo cat /etc/group
-
To add new group,
sudo groupadd public
-
To add user to a group,
sudo usermod -G public -a ftpuser
-
To check the 'ftpuser' (user ID) belongs to which group,
id ftpuser
-
To check my user ID
whoami
Switching to another user context
16.Jul.2025: sometimes, you want to execute a bash script with a dedicated user ID for the reason of any new files or directories to be created will be under this user ID without having to execute chown
. Another advantage is that you are allowed to set the file modification time because the file owner is same as the user who is running the script.
For example, you need to upgrade an application while we want to allow the 'myftpuser' (or any other user ID) to access the new directory created by the script.
runuser -l myftpuser -c "./tools/util/upgrade-app.sh"
Where upgrade-app.sh
is calling rsync
to copy new files and set the file modification time.
More about security
-
17.Jan.2025: about sudoer
The following article explains how to add an user to the sudo group without modifying the
/etc/sudoers
file,https://www.digitalocean.com/community/tutorials/how-to-create-a-new-sudo-enabled-user-on-ubuntu
For using
sudoers
file,https://www.digitalocean.com/community/questions/a-deep-dive-into-the-sudoers-file
The
su
(switch user) command,
Related posts
Back to #UBUNTU blog
Back to #blog listing
Author
Lau Hon Wan, software developer.