The `chmod` and `chown` Commands in Linux

Description

chmod (change mode) — a command for modifying file and directory permissions in Linux.
chown (change owner) — a command for changing the owner and group of a file or directory.


Syntax

chmod

chmod [options] mode file
  • mode — permissions (numeric or symbolic format).
  • file — target file or directory.

chown

chown [options] user[:group] file
  • user — new file owner.
  • group (optional) — new file group.
  • file — target file or directory.

Permission Formats

chmod — Numeric (Octal) Format

Each permission type corresponds to a number:

  • 4 — read (r)
  • 2 — write (w)
  • 1 — execute (x)

Example:

chmod 755 myfile

Means:

  • Owner: rwx (7 = 4+2+1)
  • Group: r-x (5 = 4+1)
  • Others: r-x (5 = 4+1)

chmod — Symbolic Format

Format:

chmod [category][operation][permissions] file

Where:

  • Category: u (user/owner), g (group), o (others), a (all).
  • Operation: + (add), - (remove), = (set exactly).
  • Permissions: r (read), w (write), x (execute).

Example:

chmod u+x myscript.sh

Adds execute permission for the owner.


chown — Changing Ownership

Change file owner:

chown user myfile

Change owner and group:

chown user:group myfile

Change owner and group recursively for a directory:

chown -R user:group my_directory

Common Examples

chmod

  • Allow execution for all users:

    chmod +x script.sh
    
  • Full permissions for owner, none for others:

    chmod 700 private_file
    
  • Give group write access:

    chmod g+w shared_folder
    
  • Set exact permissions for all:

    chmod u=rwx,g=rx,o=rx public_file
    

chown

  • Change file owner:

    chown alice myfile
    
  • Change owner and group for a directory recursively:

    chown -R alice:developers my_directory
    

Recursive Permission Changes

chmod

Apply to a directory and all its contents:

chmod -R 755 my_directory

Useful Options

  • chmod -v — verbose output of changes.
  • chmod -c — output only changed files.
  • chmod --reference=file — apply the same permissions as another file.
  • chown --from=olduser newuser file — change owner only if current owner matches olduser.
  • chown --reference=file file2 — set owner and group same as another file.

Security Tips

  1. Avoid granting excessive permissions (especially 777).
  2. For scripts, 755 is usually sufficient.
  3. Protect sensitive files with 600.
  4. Change file ownership only when necessary.

These commands form the foundation of access control in Linux, essential for both security and proper system operation.


Conclusion

The chmod and chown commands are powerful tools for managing access to files and directories. They allow you to define permissions in either numeric or symbolic form, making them essential for Linux system administration.

UA EN RU

Зв'язатися з нами

Telegram Email