Important
How do I use chmod and chown command under Linux / Unix operating systems?
Use the chown command to change file owner and group information. we run the chmod command command to change file access permissions such as read, write, and access. This page explains how to use chmod and chown command on Linux or Unix-like systems.
Markdown
Understanding file permissions for chmod and chown command.
One can use file permissions to control access to their files. Sysadmins can
enforce a security policy based upon file permissions. All files have three types:
read ®, write (w), and execute (x) permission
We can set the following permissions on both files and directories:
| Permission | File | Directory |
|---|---|---|
| r | Reading access/view file | Users can read file. In other words, they can run the ls command to list contents of the folder/directory. |
| w | Writing access/update/remove file | Users can update, write and delete a file from the directory. |
| x | Execution access. Run a file/script as command | Users can execute/run file as command and they have r permission too. |
| - | No access. When you want to remove r, w, and x permission | All access is taken away or removed. |
Please Note that Permission Priority decided as follows by the kernel:
- We can set the following permissions on both files and directories:
User permissions -> Group permissions -> Other permissions - It means user permission overrides group permission and group permissions overrides other permission.
Viewing Linux/Unix file permissions and ownership.⚓︎
Viewing Linux file Permissions
Run the ls command:
Show information about a file named file1 #
Get information about a directory named dir1 #
List Permissions
For example, we can list permissions for /etc/hosts and /etc/ directory as follows:
Pass the -d option to ls to list directories themselves, not their contents:
-rw-r--r-- 1 root root 742 Jul 1 14:39 /etc/host
ls -l -d /etc/
drwxr-xr-x 175 root root 12288 Jul 30 08:53 /etc
From above outputs it is clear that the first character indicate the file type in drwxr-xr-x and -rw-r–r– and the next 9 characters are the actual file permissions.
–rw-r–r– file and drwxr-xr-x directory permission explained
| First character | Description |
|---|---|
| - | Regular file. |
| b | Block special file. |
| c | Character special file. |
| d | Directory. |
| l | Symbolic link. |
| p | FIFO. |
| s | Socket. |
| w | Whiteout. |
Next nine characters are the file permissions divided into three sets/triad of three characters for owner permissions, group permissions, and other/world permissions as follows:
| Three permission triads defined what the user/group/others can do | First triad defines what the owner can do | Second triad explains what the group members can do | Third triad defines what other users can do |
|---|---|---|---|
| -rw-r--r-- | Owner has only read and write permission (rw-) | Group has read permission (r--) | Others has read permission (r--) |
| drwxr-xr-x | Owner has full permission (rwx) | Group has read and execute permission (r-x) | Others has read and execute permission (r-x) |
Displaying file permission using the stat command⚓︎
Stat Command
Run the following command:
File: /etc/passwd
Size: 3100 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 25954314 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-07-29 23:09:01.865822913 +0530
Modify: 2020-07-02 19:16:43.743727913 +0530
Change: 2020-07-02 19:16:43.747727898 +0530
Birth: -
GUI displaying file permissions:⚓︎
Chown Command⚓︎
Chown Command
The chown command changes the user and/or group ownership of for given file. The syntax is:
Examples⚓︎
Examples
First, list permissions for demo.txt, enter:
In this example change file ownership to vivek user and list the permissions, run:
Next Example
In this next example, the owner is set to vivek followed by a colon and a group onwership is also set to vivek group, run:
# chown vivek:vivek demo.txt
# ls -l demo.txt
Sample outputs:
-rw-r--r-- 1 vivek vivek 0 Aug 31 05:48 demo.txt
In this example, change only the group of file. To do so, the colon and following GROUP-name ftp are given, but the owner is omitted, only the group of the files is changed:
Colon Only
Please note that if only a colon is given, or if NEW-OWNER is empty, neither the owner nor the group is changed:
# chown : demo.txt
In this example, change the owner of /foo to “root”, execute:
# chown root /foo
Likewise, but also change its group to “httpd”, enter:
# chown root:httpd /foo
Change the owner of /foo and subfiles to “root”, run:
# chown -R root /u
Where, -R – Recursively change ownership of directories and their contents.
Chmod Command⚓︎
Chmod Command
The syntax is:
chmod permission file
chmod permission dir
chmod User AccessRights Permission file
We use the following letters for user:
u for userg for groupo for othersa for all
We can set or remove (user access rights) file permission using the following letters:
+ for adding- for removing= set exact permission
File permission letter is as follows:
r for read-onlyw for write-onlyx for execute-only
Examples
Now we can use the symbolic method for changing file permissions based upon the above letters.
- Examples
Delete,readandwritepermission for group and others on a file named config.php:
Let us add read permission for all/everyone (a). In other words, give read permission to user, group and others:
Next, sets read and write permission for user, sets read for group, and remove all access for others:
In this file example, sets read and write permissions for user and group:
See “how to use change user rights using the chomod command" for more information.
Conclusion⚓︎
We explained the chown and chmod command for Linux and Unix users. I strongly suggest that you read man pages by typing the following man command or see GNU coreutils online help pages: