File permissions.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,511
Reaction score
3,256
Credits
31,430

Understanding and Using chmod to Change File Permissions in Linux​

Introduction​

In Linux, file permissions are crucial for maintaining system security and functionality. The chmod command is used to change the permissions of files and directories. This article will explain how to use chmod, what the rwxr-xr-x notation means, and the implications of changing file permissions.

File Permissions Explained​

When you run ls -l to list files in a directory, you'll see something like this:

-rwxr-xr-x 1 user group 1234 Jan 4 07:34 example.sh

The first part, -rwxr-xr-x, represents the file permissions. Here's what each part means:

  • -: Indicates the type of file. A - means a regular file, d means a directory, and l means a symbolic link.
  • rwx: Permissions for the owner (read, write, execute).
  • r-x: Permissions for the group (read, execute).
  • r-x: Permissions for others (read, execute).
Sometimes, you might see something like drwxr-xr-x or lrwxr-xr-x:

  • d: Indicates a directory.
  • l: Indicates a symbolic link.

Differences Between ls and ls -l​

  • ls: Lists the files and directories in the current directory.
  • ls -l: Provides a detailed list, including file permissions, number of links, owner, group, size, and timestamp.

Understanding Permissions​

  • Read-only (r): Allows viewing the contents of a file.
  • Read/write (rw): Allows viewing and modifying the contents of a file.
  • Executable (x): Allows executing the file as a program.

Using chmod​

There are two main ways to use chmod:

  1. Symbolic Mode: Uses symbols to represent changes.
    • Example:
      Code:
      chmod +x example.sh
      (adds execute permission for everyone).
    • Example:
      Code:
      chmod g-w example.sh
      (removes write permission for the group).
  2. Numeric Mode: Uses numbers to represent permissions.
    • Example:
      Code:
      chmod 755 example.sh
      (sets permissions to rwxr-xr-x).
    • Example:
      Code:
      chmod 644 example.sh
      (sets permissions to rw-r--r--).

Changing Permissions for Owner, Group, and Others​

  • Owner:
    Code:
    chmod u+rwx example.sh
    (adds read, write, and execute permissions for the owner).
  • Group:
    Code:
    chmod g+rx example.sh
    (adds read and execute permissions for the group).
  • Others:
    Code:
    chmod o+r example.sh
    (adds read permission for others).

Sticky Bit​

The sticky bit is a special permission that can be set on directories. When the sticky bit is set, only the file's owner, the directory's owner, or the root user can delete or rename the files within that directory. This is commonly used on directories like /tmp to prevent users from deleting each other's files.

To set the sticky bit, you can use:

  • Code:
    chmod +t /path/to/directory

Risks of Changing System File Permissions​

Changing permissions on system files can lead to serious issues. For example:

  • Removing execute permissions from essential binaries:
    Code:
    chmod -x /bin/ls
    will prevent the ls command from running.
  • Changing permissions on configuration files:
    Code:
    chmod 777 /etc/passwd
    makes the file writable by anyone, posing a security risk.
Always be cautious when modifying permissions, especially on system files, to avoid breaking your system.
 


Members online


Latest posts

Top