Adding a User to a Group in Linux (usermod)
You will learn how to create a group, assign an existing user to a group, and create a new user and add them to a group. You will also learn how to add a user to the sudoers group. You will need an account with root privileges.
Add or Delete a Group
Use the groupadd command for this. Root privileges are required to run it. It is simple to use and does not require installing anything else on the system.
sudo groupadd <nazwagrupy>To delete a group, use the groupdel command. Its syntax is:
sudo groupdel <nazwagrupy>Display All Groups in the System
Use the getent group command. It lists all groups in the system, shows their ID numbers, and indicates whether a given user belongs to them.
getent groupAdd an Existing User to a Group
Use the usermod command for this. The user account must already exist in the system. Root privileges are also required here. The general syntax is:
sudo usermod -a -G <nazwagrupy> <nazwausera>You can also use the usermod command to add a user to several groups at once. Just list the groups separated by commas.
sudo usermod -a -G <nazwagrupy>, <nazwagrupy2>, <nazwagrupy3> <nazwausera>To add a user to the sudoers group, you need to switch to the root account and authenticate with its password. Use the su command for this. Adding a user to the sudoers group automatically gives them root privileges because of the configuration for this group in the /etc/sudoers file.
su
usermod -a -G sudo <nazwausera>Create a User and Add Them to a Group
To add a user to a group while creating the account, use the useradd command with the -G parameter. Remember that if a user with the given name already exists in the system, they will only be assigned to the new group rather than created again.
sudo adduser <nazwausera> <nazwagrupy>Display the Groups a User Belongs To
Use the groups command.
groupsTo display the groups another user belongs to, use the groups command with the username as a parameter.
groups <nazwausera>You can also display the groups a given user belongs to together with their ID numbers. Use the id command with the username as a parameter.
id <nazwausera>Comments (0)
No comments yet.
Add a comment
Comments are published after moderation. Your e-mail address stays private.