Linux - chmod Command

1. pwd Command (for completeness)

pwd stands for Print Working Directory.
It displays the full path of the directory you are currently in.

Example:

pwd

Output:

/home/user/Documents

2. useradd / userdel in Linux User Management

useradd

useradd is used to create a new user account in Linux.

Basic Syntax

useradd username

Common Options

useradd -m username   # Create home directory
useradd -s /bin/bash username   # Set default shell
useradd -g groupname username   # Assign a primary group

Example

useradd -m -s /bin/bash john

Creates user john with a home directory and bash shell.


userdel

userdel is used to delete a user account.

Basic Syntax

userdel username

Delete user with home directory

userdel -r username

-r removes the user's home directory and mail files.


3. groupadd / groupdel in Linux

groupadd

Creates a new group in the system.

Syntax

groupadd groupname

Example

groupadd developers

groupdel

Deletes an existing group.

Syntax

groupdel groupname

Example

groupdel developers

Note: You cannot delete a group if it's still assigned as a primary group to any user.


4. Understanding sudo Privileges

sudo stands for SuperUser DO.
It allows a normal user to run commands with root (administrator) privileges.

Why sudo is used?

  • Provides controlled administrative access

  • Prevents giving full root access to all users

  • Logs all admin actions

Give a user sudo privileges

Add the user to the sudo group:

usermod -aG sudo username

Using sudo

sudo command

Example:

sudo apt update

Users must enter their password to confirm.


5. Explaining chmod, File Permissions & Access Control

File Permissions in Linux

Every file and directory has 3 types of permissions:

  1. Read (r)

  2. Write (w)

  3. Execute (x)

Permissions apply to:

  • User (owner)

  • Group

  • Others (everyone else)

Example of permissions

-rwxr-xr--

Meaning:

  • User: rwx (read, write, execute)

  • Group: r-x (read, execute)

  • Others: r-- (read only)


chmod Command (Change File Permissions)

chmod is used to change permissions of files and directories.

Two Methods

1. Symbolic method

chmod u+x file.txt        # Add execute for user
chmod g-w file.txt        # Remove write for group
chmod o+r file.txt        # Add read for others
chmod u=rwx,g=rx,o=r file.txt

2. Numeric method

Each permission has a number:

  • r = 4

  • w = 2

  • x = 1

Add the values:

User Group Others
7 (r+w+x) 5 (r+x) 4 (r)
chmod 754 file.txt

Useful chmod examples

Make a file executable

chmod +x script.sh

Full permissions to user only

chmod 700 securefile

Read/write for everyone

chmod 666 file.txt