How To Use chmod and chown Command

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.

ADVERTISEMENTS

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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
33 comments… add one
  • E. Hill Oct 29, 2006 @ 14:59

    Excellent site!! Your explanations are great. Clear and concise and very helpful! Thanks!

  • somery.van May 15, 2008 @ 7:53

    it so good but the result it not enough so i want you add more information.
    thank you

  • Derrick Jul 1, 2009 @ 19:53

    don’t know where you got this but all you get is an error that the directory does not exist. so if this script is working for you, then your server is set up wrong and you are riding or borrowed time.

  • Derrick Nov 13, 2009 @ 6:39

    doesn’t work

  • ubi Jan 14, 2010 @ 6:10

    nice lesson =)

  • sabaresh Oct 18, 2010 @ 8:54

    its a good one for the ones who use linux for the first time…..can read and learn

  • SilverBear May 15, 2011 @ 16:46

    Good lesson as far as it goes. However:
    This command will only work if the file targeted by the command exists. You can’t change ownership of a non-existent file!

  • Hitesh Mar 15, 2012 @ 13:10

    Hello,

    I am Hitesh, i am a S/W Engineer, My nickname is CSD.
    Linux is useless operating system.

    I am the owner of Tata Magic IRIS. I don’t need this command to change anything.
    I am the ROOT.

    You all people are FOOL.

    • NoNeedForAName Apr 2, 2012 @ 1:56

      Hello,

      I am Hitesh, i am ID10T who uses everybody’s valuable oxygen.
      Intelligence is useless concept me.

      I am the owner of pathetic little car. I don’t have anything contribute to site.
      I am the MORON.

      You all people are TOOL.

      In other words: if you don’t understand proper english, don’t try to act like you do, you little numpty.

      • abcd Jan 8, 2016 @ 12:28

        nice one. :)

  • Kc5jmr Apr 13, 2012 @ 2:40

    What if I want to give permission to everyone
    /home/storage
    I need it so anyone who access from a windows share via network neighborhood can read and write to this directory from any location

    Robert

  • SilverBear Apr 15, 2012 @ 19:01

    Hi, Kc5jmr.
    The chmod and chown commands are really for use to fine-tune permissions among users of a single filesystem [–usually, but not always, a single computer with multiple users.].

    What you’re talking about is setting up a Samba [SMB] fileserver for network sharing of files. That is really a whole different project from just adjusting file permissions for various logged-in users on the same machine!

    I use Samba to access files on my desktop workhorse from my portable computer. Setting up a Samba server and writing a config file that specifies the directories to be shared is not all that difficult.

    BUT SMB was a Windows protocol devised in the middle 1980’s. To the best of my understanding, Windows 7 uses some other setup. All I know is that my Win7 OS on my portable can’t connect to my Linux samba server, but booting the same machine in Mepis Linux allows me full access. Since I only boot into Win7 to play “Sid Meyer’s Civilization” [versions IV & V] the lack of backward compatibility to SMB/Samba really doesn’t affect me enough for me to bother to try to get Win7 to play nice. I do all my serious work in Linux, and only have so much time/energy for gratuitous MS-Windows troubleshooting!

  • Newbie May 9, 2012 @ 16:27

    Great website i found the solution for many of my problems here :) Wish you luck and success !

  • Alan T Jun 29, 2012 @ 1:57

    Hitesh, don’t comment here you obviously no nothing about system administration.

    And if your using a development system\environment that “requires” a root account so your can write software your the FOOL.

  • LinuxUsers Jun 29, 2012 @ 13:20

    Hi all,

    Please stop feeding the troll that is Hitesh.

    Sincerely,

    Real Linux and FOSS users.

  • Mac Oct 25, 2012 @ 10:28

    Hi Guys

    can anyone assist, i am getting an error when trying to start one of my applications on my linux server.

    ./pwcallmgr: error while loading shared libraries: libjs.so: cannot restore segment prot after reloc: Permission denied

  • Hilary Nov 19, 2012 @ 16:48

    you have over written the permissions because these instructions were poorly written open up your Terminal navigate tothe root file system and add yourself as a user allowed to open this file… having followed these instructions with teh recursive you have possibly told your OS that only root can open that file when you need to be the one who does. use grep to find the for or just type in find ./pwcallmgr once you have this file chown yourusername ./pwcallmgr if it says not permitted the sudo chown yourusername ./pwcallmgr should do it.

    Be very careful when using the -Recursive switch or you could end up having to boot using a live disk to fix this …. and make your computer unbootable…
    You can also use Chmod 777 if you need to however you should check what file permissions that file is supposed to have.

    Of course if you have a full back up on another drive of your .hidden files and your home file along with any other files you like to keep then it can be faster just to reinstall with the latest OS…. but I would back things up just in case and try the chown first ….

  • Udayjeet The Fututre Coder Jan 14, 2013 @ 6:29

    Thanks, as i always prefer this website in the case of unix commands or scripts.

    Thanks to this website.

    As i get the 100% correct solutions.

  • Udayjeet The Future Coder Jan 14, 2013 @ 6:30

    sorry the name gone wrong ………..oops

    Udayjeet “THE FUTURE CODER”

  • marty Mar 3, 2013 @ 6:38

    “chown -R” frightens me. When I’m admin’ing I’m often needed to change the ownership of files owned by “bob” to “bill” in filesystems with files owned by a number of accounts.

    I would typically (from memory):

    find . -user bob -exec chown bill {} \;

  • cheran Aug 13, 2013 @ 12:48

    excellent stuff bro .. did my job .. so user friendly .. keep it up

  • Elias Nov 9, 2013 @ 13:05

    How to change d05 directory ownership from root to oracle

    • biscuit Nov 29, 2013 @ 4:21

      chown -R oracle:oracle d05 (path to d05 directory)

  • l0xy Jul 23, 2014 @ 18:03

    Hello. At the last line of this guide where you have “/u” did you possibly mean “/foo” ??
    To be consistent, perhaps this is what you rather meant?

  • Pradeep Nov 7, 2014 @ 12:16

    I have directory ‘/u02/Infa_shared’, created one group ‘Developers’ and Added users ‘A’,’B’. Owner of this directory is ‘infaadmin’ and user belongs to ‘admin’ group.

    How can I give read, write, execute access on this directory to ‘Developers’ by keeping ‘admin’ group has its owner in Linux?

  • sreenath May 13, 2015 @ 9:38

    it is not working in my lap is there any possible to change owner & group

  • Brian Lennon Jun 6, 2015 @ 17:16

    Really good explanation of the basics of chown/chmod! Good work!

  • selladurai Jun 30, 2015 @ 22:12

    i have one doubt, any one help me. i have created one parent directory (sample). that directory owner is “test”, now i created many sub directories under sample. now i want changed sub directory owner.
    Note : No changes to parent directory.

  • Ram Aug 1, 2015 @ 3:46

    These are very helpfull to learners thank u !!!

  • amanjadoon Nov 23, 2015 @ 20:25

    i have pasted a directory(grid folder) from windows 10 into may Linux machine

    [root@localhost Desktop]# cd /u01
    [root@localhost u01]# ls
    grid lost+found
    [root@localhost u01]# cd grid
    [root@localhost grid]# ls
    install response runcluvfy.sh sshsetup welcome.html
    readme.html rpm runInstaller stage
    [root@localhost grid]# ls -l
    total 68
    drwxr-xr-x. 4 root root 4096 Nov 24 00:45 install
    -rw-r–r–. 1 root root 30016 Aug 26 2013 readme.html
    drwxr-xr-x. 2 root root 4096 Nov 24 00:45 response
    drwxr-xr-x. 2 root root 4096 Nov 24 00:45 rpm
    -rw-r–r–. 1 root root 4878 Aug 26 2013 runcluvfy.sh
    -rw-r–r–. 1 root root 3268 Aug 26 2013 runInstaller
    drwxr-xr-x. 2 root root 4096 Nov 24 00:45 sshsetup
    drwxr-xr-x. 14 root root 4096 Nov 24 00:48 stage
    -rw-r–r–. 1 root root 500 Aug 26 2013 welcome.html

    now i wanna run the runInstaller to Install it but i can’t what can i do ????

    and i can’t access my shared folder from windows to linux through vmware i have enabled the folder for sharing but when i type

    [root@localhost Desktop]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/sda1 20G 5.3G 13G 30% /
    tmpfs 1002M 224K 1002M 1% /dev/shm
    /dev/sda3 35G 1.3G 32G 4% /u01
    /dev/sr0 3.7G 3.7G 0 100% /media/OL6.5 x86_64 Disc 1 20131125
    [root@localhost Desktop]#

    the /mnt/hgfs folder should b visible but it can’t can solve my problem ???

  • Ninju Bohra Aug 2, 2016 @ 17:15

    On that last example should it be
    # chown -R root /foo
    (to match the description)

  • guest Nov 9, 2016 @ 8:17

    Nice document for beginners.Thanks a lot :)

  • Seyit Ahmet Tanrıver Jul 30, 2020 @ 2:23

    Great content, very useful thank you.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.