Mastering Linux User Accounts, Groups, and File Permissions

Proper user and permission management is a crucial part of Linux system security and organization. This is the second part of the Linux series. We'll explore user accounts, groups, and file permissions in a detailed way. You'll learn how to create and manage users, assign groups, and control file access with permissions.

Summary Table

Command Description
useradd <user>"Create a new user account."
passwd <user>Set or change a user's password
getent passwdDisplay all user entries.
cat /etc/passwdShow all user entries (file view)
id <user>Show UID, GID, and groups for a user
su - <user>Switch to another user (login shell)
sudo <command>Run command as root (if configured)
userdel <user>Delete a user account
groupadd <group>Create a new group
getent groupShow all groups
cat /etc/groupShow groups (file view)
groupdel <group>Delete a group
usermod -aG <group> <user>Add a user to a group
chown <user> <file>Change file owner
chown <user> <file1> <file2>Change owner of multiple files
chown <user> * Change owner of all files in directory
chgrp <group> <file>Change group ownership of file
chgrp <group> * Change group for all files
chown <user>:<group> <file>Change owner and group for file
chown <user>:<group> * Change owner and group for all files
chown -R <user>:<group> <folder>Recursively change owner/group of folder and contents
chmod 777 <file>Give read/write/execute to all
chmod 751 <file>Set specific permissions (rwxr-x--x)
chmod -R 777 <folder>Recursively set all permissions for folder

Linux User, Group, and Permission Commands

1. User Management Commands

  • useradd <user>
    # Create a new user
    sudo useradd john
    
    # Create user with home directory and default shell
    sudo useradd -m -s /bin/bash john
        

    Creates a new user account.

    • -m: Create home directory
    • -s: Specify login shell
  • passwd <user>
    sudo passwd john

    Sets or changes the password of a user. The root user can change any user's password.

    Sample output:

    New password:
    Retype new password:
    passwd: password updated successfully
        
  • getent passwd / cat /etc/passwd
    getent passwd

    Shows all user account entries from the system’s account database.

    Sample output:

    root:x:0:0:root:/root:/bin/bash
    alice:x:1001:1001:Alice:/home/alice:/bin/bash
        
  • id <user>
    id john

    Shows the UID, GID, and group memberships of the specified user.

    Sample output:

    uid=1001(john) gid=1001(john) groups=1001(john),27(sudo)
        
  • su - <user>
    su - john

    Switches to another user account and loads that user's environment.

  • sudo <command>
    sudo apt update

    Runs a command with elevated privileges if the current user has sudo access.

    Sample output:

    Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
    Reading package lists... Done
        
  • userdel <user>
    sudo userdel john

    Deletes a user account from the system.


2. Group Management Commands

  • groupadd <group>
    sudo groupadd developers

    Creates a new group on the system.

  • getent group / cat /etc/group
    getent group

    Displays all groups in the system’s account database.

    Sample output:

    root:x:0:
    developers:x:1002:alice
        
  • groupdel <group>
    sudo groupdel developers

    Deletes a group from the system.

  • usermod -aG <group> <user>
    sudo usermod -aG developers john

    Adds a user to a supplementary group without removing them from other groups.

    • -a: Append to existing groups
    • -G: Specify supplementary groups

3. File Ownership Commands

  • chown <user> <file>
    sudo chown john file.txt

    Changes the ownership of a file to the specified user.

  • chown <user> <file1> <file2>
    sudo chown john file1.txt file2.txt

    Changes the ownership of multiple files at once.

  • chown <user> *
    sudo chown john *

    Changes the ownership of all files in the current directory to the specified user.

  • chgrp <group> <file>
    sudo chgrp developers file.txt

    Changes the group ownership of a file.

  • chgrp <group> *
    sudo chgrp developers *

    Changes the group ownership of all files in the current directory.

  • chown <user>:<group> <file>
    sudo chown john:developers file.txt

    Changes both the owner and group of a file.

  • chown <user>:<group> *
    sudo chown john:developers *

    Changes both the owner and group for all files in the current directory.

  • chown -R <user>:<group> <folder>
    sudo chown -R john:developers myfolder

    Recursively changes the owner and group of a folder and all its contents.

    • -R: Apply changes recursively

4. File Permission Commands

  • chmod 777 <file>
    chmod 777 file.txt

    Gives read, write, and execute permissions to all users for the file.

  • chmod 751 <file>
    chmod 751 file.txt

    Sets the file permissions to:

    • rwx for owner (7)
    • r-x for group (5)
    • --x for others (1)
  • chmod -R 777 <folder>
    chmod -R 777 myfolder

    Recursively sets full read, write, and execute permissions for all files and subdirectories inside a folder.

    • -R: Apply changes recursively
devtutspro

At DevtutsPro, we focus on delivering high-quality, simplified content around DevOps tools, workflows, and cloud-native development. We publish project-based tutorials and tool explanations to help you build a solid foundation in DevOps, automate deployments, and understand how modern infrastructure works — all using real examples in AWS.

Post a Comment (0)
Previous Post Next Post