Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Manage Users in Linux: A Comprehensive Guide

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.

Creating a New User

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

Modifying an Existing User

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.

Deleting a User

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.

Managing User Groups

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

Viewing User Information

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.

Managing Permissions

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.

Practical Example: Creating a New User and Setting Permissions

Let's create a new user, add them to a group, and set permissions for a directory.

  1. Create a new user and set a password:

    sudo useradd -m johndoe
    sudo passwd johndoe
  2. Create a new group and add the user to the group:

    sudo groupadd developers
    sudo usermod -aG developers johndoe
  3. 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.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.