User and Group Management in Ubuntu
Below are some common user and group management commands in Ubuntu.
Adding New Users
To add new users, you can use the useradd
command.
sudo useradd $username
Adding New Groups
To add new groups, you can use the groupadd
command.
sudo groupadd $groupname
Creating New Users and Assigning to a Group
When creating users with the useradd
command, you can also specify a group to add the new user to using the -G
option.
sudo useradd -G examplegroup newuser
Adding a User to a Group
To add a user to a group, you can use the usermod
command with the -a
and -G
options.
sudo usermod -a -G groupname username
To add user exampleuser
to the examplegroup
you can perform:
sudo usermod -a -G examplegroup exampleuser
You can also specify multiple groups by specifying a comma-separated value for the -G
option:
sudo usermod -a -G group1,group2,group3 exampleuser
Removing Users and Groups
To remove a user or group, you can use the following commands:
userdel
groupdel
Granting a User sudo Permissions
In Ubuntu, you can add a user to the sudo group to grant the sudo permissions.
sudo usermod -a -G sudo username
You can also add the user to the sudoers file:
echo "username ALL=(ALL) ALL" | sudo tee /etc/sudoers.d/username
Allowing Limited sudo Commands
To limit the sudo commmands available to the user, you can specify comma-separated values in the sudoers file. The following command settings only allows the user to use mkdir
and rmdir
:
echo "username ALL=(ALL) mkdir,rmdir" | sudo tee /etc/sudoers.d/username
No Password sudo Command
To remove the password prompt in sudo commands, you can add NOPASSWD
in the sudoers file:
echo "username ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/username
To do this for the current user you are logged in:
echo "$USER ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$USER