How To Use chmod and chown Command in Linux

See all GNU/Linux related FAQ
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.

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:

  1. Owner – Person or process who created the file.
  2. Group – All users have a primary group, and they own the file, which is useful for sharing files or giving access.
  3. Others – Users who are not the owner, nor a member of the group. Also, know as world permission.

read (r), 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:
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

Run the ls command:
ls -l
# Show information about a file named file1 #
ls -l file1
ls -l /path/to/file1
# Get information about a directory named dir1 #
ls -ld dir1
ls -l -d /path/to/dir1

For example, we can list permissions for /etc/hosts and /etc/ directory as follows:
ls -l /etc/hosts
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

Run the following command:
stat file1
stat dir1
stat /etc/passwd
stat /etc/resolv.conf

  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:
Chmod and Chown in GUI

chown command

The chown command changes the user and/or group ownership of for given file. The syntax is:

chown owner-user file 
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file

Examples

First, list permissions for demo.txt, enter:
# ls -l demo.txt
Sample outputs:

-rw-r--r-- 1 root root 0 Aug 31 05:48 demo.txt

In this example change file ownership to vivek user and list the permissions, run:
# chown vivek demo.txt
# ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 vivek root 0 Aug 31 05:48 demo.txt

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:
# chown :ftp demo.txt
# ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 vivek ftp 0 Aug 31 05:48 demo.txt

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

The syntax is:
chmod permission file
chmod permission dir
chmod UserAccessRightsPermission file

We use the following letters for user:

  • u for user
  • g for group
  • o for others
  • a 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-only
  • w for write-only
  • x for execute-only

Now we can use the symbolic method for changing file permissions based upon the above letters.

Examples

Delete read and write permission for group and others on a file named config.php:
$ ls -l config.php
# State 'who' : g (group) and o (others)
# State what to do with 'who': - (remove)
# State permissions for 'who': r (read) and w (write)

$ chmod -v go-rw config.php
$ ls -l config.php
$ stat config.php

How to use chown command on Linux and Unix
Let us adds read permission for all/everyone (a). In other words, give read permission to user, group and others:
$ chmod a+r file.pl
Delete execute permission for all everyone (a):
$ chmod a-x myscript.sh
Adds read and execute permissions for everyone (a):
$ chmod a+rx pager.pl
Next, sets read and write permission for user, sets read for group, and remove all access for others:
$ chmod u=rw,g=r,o= birthday.cgi
In this file example, sets read and write permissions for user and group:
$ chmod ug=rw /var/www/html/data.php
See “how to use change user rights using 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:
man chown
man chmod

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

34 comments… add one
  • Thai Doan Feb 28, 2021 @ 7:32

    It’s very usefull, thank you a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.