File permissions
Introduction
File permissions in Linux determine who can access, modify, and execute files and directories. They are essential for maintaining the security and integrity of a system. In Linux, file permissions are represented by a combination of read (r), write (w), and execute (x) permissions for the owner, group, and others.
File Permission Notation
In Linux, file permissions are represented using a 10-character notation:
rwxr-xr-xEach character represents a specific permission or attribute:
- The first character indicates the file type (
-for regular file,dfor directory,lfor symbolic link, etc.) - The next three characters (
rwx) represent the owner’s permissions. - The following three characters (
rwx) represent the group’s permissions. - The last three characters (
rwx) represent the permissions for others.
Octal Notation
File permissions can also be represented using octal notation, which assigns a numeric value to each permission:
r = 4
w = 2
x = 1The sum of these values represents the overall permission. For example, rwx (read, write, and execute) would be represented as 7 (4 + 2 + 1).
Changing File Permissions
File permissions can be modified using the chmod command in Linux. The command accepts both symbolic and octal notation.
To change permissions using symbolic notation:
chmod permissions filenameTo change permissions using octal notation:
chmod 7 max # 7 gives max full authority over fileCommonly Used Permissions
Here are some commonly used permissions and their meanings:
- rwx (7): Full permissions (read, write, and execute)
- r-x (5): Read and execute permissions
- rw- (6): Read and write permissions
- r-- (4): Read-only permissions
- -wx (3): Write and execute permissions
- -w- (2): Write-only permissions
- --x (1): Execute-only permissions #cd permission into dir
- --- (0): No permissions- : filetype
--- : User
--- : group
--- : others
lrwxr-wx-rw-x 1 root root 7 Aug 24 2021 bin -> usr/bin
# file type : (l) symbolic link file pointing to usr/bin
# owner : root -> (rwx)
# group : root -> (rwx)
# others -> (rwx)
# file size : 7
# name : bin
# numeric-permission: 777 => (owner): 7 | (group): 7 | (others): 7
"Permission for link files may differ at source file for instance bin
may have diffrent permissions structure"
drwxr-xr-x 5 root root 4096 Jun 6 02:05 boot
# file type : (d) directory
# owner : root (rwx)
# group : root (r-x) -> no write permissions
# others -> (r-x )
# name : boot
# numeric-permission: 755 => (owner): 7 | (group): 5 | (others): 5
-rw------- 1 root root 1507852288 Oct 28 2021 swap.img
# read write by owner root
# (-) regular file
# owner : root (rw-) -> no execute
# group : root (---) -> no permissions
# others -> (---)
# name : swap.img
# numeric-permission: 600 (owner): 6 | (group): 0 | (others): 0
dr-xr-xr-x 13 root root 0 Jun 5 22:01 sys
# file type : directory (d)
# owner : root (r-x) read and execute
# group : root (r-x)
# size : 0
# other -> (r-x)
# numeric-permission: 555 (owner): 5 | (group): 5 | (other): 5chmod
The chmod command is used in Unix and Linux systems to change the permissions of files and directories. It stands for “change mode.”
Syntax
The basic syntax of chmod is as follows:
chmod [options] mode fileoptions: Various options can be used to modify the behavior of the command.mode: Specifies the new permissions to be set for the file or directory.file: Specifies the target file or directory for which permissions are to be modified.
Permissions
There are two common methods to represent permissions in chmod:
-
Symbolic Method:
- The permissions are represented by three sets of characters, each containing three letters: user (owner), group, and others.
- The letters used are
rfor read,wfor write, andxfor execute. - The symbols
+,-, and=are used to add, remove, or set permissions explicitly. - Example:
chmod u+rwx file(Adds read, write, and execute permissions for the owner of the file).
-
Numeric Method:
- The permissions are represented by a three-digit number.
- Each digit represents the combined value of read (4), write (2), and execute (1) permissions.
- The digits are calculated for user (owner), group, and others.
- Example:
chmod 755 file(Sets read, write, and execute permissions for the owner, and read and execute permissions for group and others).
Examples
Here are a few examples of how chmod can be used:
-
Symbolic Method:
chmod go-w file(Removes write permission for both the group and others).chmod a=r file(Sets read permission for all - owner, group, and others - while removing write and execute permissions).
-
Numeric Method:
chmod 644 file(Sets read and write permissions for the owner, and read-only permissions for group and others).chmod 700 file(Sets read, write, and execute permissions for the owner only).
Conclusion
chmod is a powerful command that allows users to modify file and directory permissions in Unix and Linux systems. It provides granular control over access rights, enhancing security and privacy.
chown
The chown command is used in Unix and Linux systems to change the ownership of files and directories. It stands for “change owner.”
Syntax
The basic syntax of chown is as follows:
chown [options] owner[:group] file
- options: Various options can be used to modify the behavior of the command.
- owner: Specifies the new owner of the file or directory.
- group: (Optional) Specifies the new group ownership of the file or directory. If not specified, the group remains unchanged.
- file: Specifies the target file or directory for which ownership is to be changed.
Ownership
The owner parameter specifies the new owner of the file or directory. It can be specified either as a user name or as a numeric user ID (UID).
The group parameter, when provided, specifies the new group ownership of the file or directory. It can be specified as a group name or as a numeric group ID (GID).
Examples
Here are a few examples of how chown can be used:
- chown user1 file: Changes the owner of the file to user1 while leaving the group ownership unchanged.
- chown user1:group1 file: Changes both the owner and group ownership of the file to user1 and group1, respectively.
- chown :group2 file: Changes the group ownership of the file to group2 while leaving the owner unchanged.
- chown 1001:1001 file: Changes both the owner and group ownership to the numeric user ID (UID) and group ID (GID) 1001, respectively.