Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Managing users in a Linux environment is a fundamental task for system administrators. This guide will walk you through the essential commands and practices for user management in Linux, including creating, modifying, and deleting users, as well as managing user groups and permissions.
To create a new user in Linux, you can use the useradd
command. This command adds a new user to the system.
sudo useradd -m newuser
-m
: Creates the user's home directory.After creating the user, you should set a password for the new account using the passwd
command:
sudo passwd newuser
To modify an existing user, you can use the usermod
command. For example, to change the user's login name:
sudo usermod -l newlogin oldlogin
Or to add the user to a new group:
sudo usermod -aG groupname username
-aG
: Adds the user to the specified group.To delete a user, you can use the userdel
command. To remove the user and their home directory:
sudo userdel -r username
-r
: Removes the user's home directory and mail spool.Groups are used to manage permissions for multiple users. To create a new group, use the groupadd
command:
sudo groupadd groupname
To add a user to a group, use the usermod
command as shown earlier. To remove a user from a group:
sudo gpasswd -d username groupname
To view information about a specific user, you can use the id
command:
id username
This command displays the user ID (UID), group ID (GID), and group memberships.
Permissions in Linux are managed using the chmod
, chown
, and chgrp
commands. For example, to change the owner of a file:
sudo chown newowner filename
To change the group ownership of a file:
sudo chgrp newgroup filename
To change the permissions of a file:
chmod 755 filename
755
: Sets the permissions to read, write, and execute for the owner, and read and execute for the group and others.Let's create a new user, add them to a group, and set permissions for a directory.
Create a new user and set a password:
sudo useradd -m johndoe
sudo passwd johndoe
Create a new group and add the user to the group:
sudo groupadd developers
sudo usermod -aG developers johndoe
Create a directory and set permissions:
sudo mkdir /project
sudo chown johndoe:developers /project
sudo chmod 770 /project
In this example, the /project
directory is owned by johndoe
and the developers
group, with read, write, and execute permissions for both the owner and the group.