- Published on
File Ownership and Permission in Unix
- Authors
- Name
- Yisak Abraham
- @YisakAbrahamK
Introduction
File ownership and permissions are fundamental concepts in Unix-like operating systems. Understanding how file ownership and permissions work is essential for system administrators and users to manage files and directories effectively. UNIX is multi-user operating system which can be accessed by many users simultaneously but this raises security concerns as user can corrupt, change or remove crucial data. For effective security, Unix divides authorization into two levels Ownership and Permission
Ownership of Unix files
Every file and directory on your Unix/Linux system is assigned three types of owner user, group and other
- A user is the owner of the file, by default the person who created a file becomes its owner. Hence, a user is also sometimes called an owner.
- Group-user in Unix is a group of multiple users. All users belong to a group will have the same access to the file.
- Others is any user who has access to a file. These users have neither created the file, nor they belongs to a user group who could own the file. Practically, it means everybody else. when you set the ownership for others, it is also referred as set ownership for the world.
In a Unix shell, ownership of a file or directory can be set using the ‘chown’ command. The syntax for the command is as follows:
$ chown [options] NEW_OWNER:NEW_GROUP FILE_NAME
Where
- NEW_OWNER is the new owner of the file or directory and
- NEW_GROUP is the new group that will own the file or directory.
- FILE_NAME is the name of the file or directory whose ownership needs to be changed.
NOTE
The option -R can be used to change ownership of the specified directory and all its contents recursively.
To set a user owner separately we pass the username that we want to own the file followed by a name of a file
$ chown USERNAME FILE_NAME
To set a group owner separately we pass the group name that we want to own the file followed by a name of a file
$ chown :GROUP_NAME FILE_NAME
Changing group owner using chgrp
command
$ chgrp GROUP_NAME FILE_NAME
Permissions in Unix
Permissions in Unix-like systems are assigned to files and directories for three types of users: owner, group, and others. Each user can have three types of permissions: read, write, and execute.
File Access Modes
The basic building blocks of Unix permissions are the read, write, and execute permissions
- Read (r): Allows the user to read the contents of the file.
- Write (w): Allows the user to modify the contents of the file.
- Execute (x): Allows the user to execute the file if it is a program or a script.
Reading permissions using ls -l
command
$ ls -l
total 1
-rw-r--r-- 1 user user 0 Jul 28 12:00 file.txt
In the above example, the first column -rw-r--r--
represents the permissions of the file. The first character -
indicates that it is a file. The next three characters rw-
represent the permissions for the owner of the file, the next three characters r--
represent the permissions for the group that owns the file, and the last three characters r--
represent the permissions for others.
Symbolic Representation of Permissions
Permissions can be represented symbolically using the following format: rwxrwxrwx
- The first three characters represent the permissions for the owner of the file.
- The next three characters represent the permissions for the group that owns the file.
- The last three characters represent the permissions for others.
Each set of three characters can be represented by the following symbols:
- r: Read permission
- w: Write permission
- x: Execute permission
Changing file/directory permissions
The chmod
command which stands for 'change mode' is used to change the permissions of files and directories in Unix-like systems. The syntax for the command is as follows:
$ chmod [options] mode file
There are two mode to use the command
Absolute (Numeric) Mode In this mode, file permissions are not represented as characters but a three-digit octal number. The table below gives numbers for all for permissions types.
Number Permission Symbol 0 No permission --- 1 Execute --x 2 Write -w- 3 Write, execute -wx 4 Read r-- 5 Read, execute r-x 6 Read, write rw- 7 Read, write Execute rwx To change the permission of a file to read, write, and execute for the owner, read and execute for the group, and read-only for others, you would use the following command:
zsh$ chmod 754 file.txt
Symbolic Mode In this mode, you can use symbols to represent the permissions that you want to change. The symbols used are:
- u: User
- g: Group
- o: Others
- a: All
- +: Add permission
- -: Remove permission
- =: Set permission
To add execute permission for the owner of the file, you would use the following command:
zsh$ chmod u+x file.txt
To remove write permission for the group that owns the file, you would use the following command:
zsh$ chmod g-w file.txt
To set read and write permissions for all users, you would use the following command:
zsh$ chmod a+rw file.txt
Conclusion
Understanding file ownership and permissions is essential for managing files and directories in Unix-like operating systems. By setting the correct ownership and permissions, you can control who can access, modify, and execute files on your system. The chown
and chmod
commands are powerful tools that allow you to change ownership and permissions of files and directories.