Skip to main content
Engineering LibreTexts

03-B.3: Modify Ownership of Files and Directories

  • Page ID
    26825
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    EXAM OBJECTIVES COVERED
    3.1 Given a scenario, apply or acquire the appropriate user and/or group permissions and ownership.

    File and Directory Ownership

    Every file and directory on a Linux system is owned by someone, and the owner has complete control to change or delete the files they own. In addition to having an owning user, a file has an owning group. In the output below of the ls -l command, we can see these three directories are owned by the user santiago and by the group santiago.

    santiago@pbmac-server:~$ ls -l
    drwxrwxr-x 2 santiago santiago  4096 Jun  4 14:18 Desktop
    drwxrwxr-x 2 santiago santiago  4096 Jun  4 14:18 Documents
    drwxrwxr-x 2 santiago santiago  4096 Jun  4 14:18 Downloads
    -rw-r--r-- 1 santiago santiago  8980 Apr 18 16:30 examples.desktop
    

    The ownership settings are set by the chmod command and controls who is allowed to perform read, write, or execute actions on a file or directory. Ownership can be changed with the chown command.

    It is often necessary to change ownership. Files and directories can live a long time on a system, but users can come and go. Ownership may also need to change when files and directories are moved around the system or from one system to another.

    If there is a group of users that work on personnel files, and the company hires a new employee, we need to make sure that this employee can view all the necessary personnel files.

    pbmac@pbmac-server $ ls -la
    drwxr-xr-x 2 pbmac pbmac  4096 Jun  4 17:58 .
    drwxrwxrwx 3 pbmac pbmac 45056 Jun  4 17:58 ..
    -rw-r--r-- 1 pbmac pbmac     0 Jun  4 17:51 PersonnelFile-1.txt

     File ownership is an important concept in Linux. The following video goes into more depth on this topic.

    The chown Command

    The chown command is used to change the file owner or group.

    Syntax:

    chown [ OPTION ] [OWNER][:[GROUP]] FILE…
    chown [ OPTION ] –reference=RFILE FILE…
    

    Command Options:

    Options Option Meaning
    -f, --silent, --quiet suppress most error messages
    --reference=RFILE (RFILE is simply some other file on the system) use RFILE's owner and group rather than specifying OWNER:GROUP values
    -R, --recursive operate on files and directories recursively

    Notice the PersonnelFile-1.txt is owned by user pbmac with the group pbmac. Unless a user can login as pbmac, or is in the group pbmac, they can do nothing other than read this file (notice the 'r' permissions for group and others). In this example the new employee is going to get ownership of both user and group of this file.

    pbmac@pbmac-server $ chown bob:personnel PersonnelFIle-1.txt
    pbmac@pbmac-server $ ls -l
    -rw-r--r-- 1 bob    personnel     0 Jun  4 17:51 PersonnelFile-1.txt
    

    With the chown command we can change both the owner, bob, and the group, personnel, with a single command by simply separating the user and group with a colon. If for some reason we want to change back to user pbmac and group pbmac there is a shortcut:

    pbmac@pbmac-server $ chown pbmac: PersonnelFIle-1.txt
    pbmac@pbmac-server $ ls -l
    -rw-r--r-- 1 pbmac pbmac     0 Jun  4 17:51 PersonnelFile-1.txt
    

    We do not have to change the owner - to change just the group put a colon in front of the group. Now members of the personnel group can edit the file
    (we also added write permissions to the file for the group):

    pbmac@pbmac-server $ chmod g+x PersonnelFIle-1.txt
    pbmac@pbmac-server $ chown :personnel PersonnelFIle-1.txt
    pbmac@pbmac-server $ ls -l
    -rw-r--r-- 1 pbmac    personnel     0 Jun  4 17:51 PersonnelFile-1.txt
    

    A few additional arguments to chown can be useful at both the command line and in a script. Just like many other Linux commands, chown has a recursive argument (-R) which tells the command to descend into the directory to operate on all files inside. Without the -R flag, only the permissions of the folder are changed, leaving the files inside it unchanged. In this example, assume that the intent is to change permissions of a directory and all its contents. Here I have added the -v (verbose) argument so that chown reports what it is doing:

    pbmac@pbmac-server $ ls -l . Personnel/
    .:
    total 4
    drwxr-xr-x 2 pbmac pbmac 4096 Jun  4 17:58 Personnel
    
    Personnel/:
    total 0
    -rw-r--r-- 1 pbmac pbmac 0 Jun  4 17:51 PersonnelFile-1.txt
    
    pbmac@pbmac-server $ chown -vR bob:personnel Personnel/
    changed ownership of 'Personnel/PersonnelFile-1.txtl' from pbmac:pbmac to  bob:personnel
    changed ownership of 'Personnel' from rom pbmac:pbmac to  bob:personnel
    

    Depending on your role, you may need to use sudo to change ownership of a file.

    The chgrp Command

    There is also a chgrp command which is used to change the group ownership of a file.

    Syntax:

    chgrp [ OPTION ] GROUP FILE…
    chgrp [ OPTION ] –reference=RFILE FILE…

    Command Options:

    Options Options Meaning
    -f, --silent, --quiet suppress most error messages
    --reference=RFILE (RFILE is simply some other file on the system) use RFILE's owner and group rather than specifying OWNER:GROUP values
    -R, --recursive operate on files and directories recursively

    If our PersonnelFile-1.txtfile has both owner and group of pbmac, and we desire to change the group to personnel we can do that.

    pbmac@pbmac-server $ ls -l
    -rw-r--r-- 1 pbmac pbmac 0 Jun  4 17:51 PersonnelFile-1.txt
    pbmac@pbmac-server $ chgrp personnel PersonnelFile-1.txt
    pbmac@pbmac-server $ ls -l
    -rw-r--r-- 1 pbmac personnel     0 Jun  4 17:51 PersonnelFile-1.txt
    

    Adapted from:
    "Introduction to the Linux chown command" by Alan Formy-Duval, opensource.com is licensed under CC BY-SA 4.0
    "chown command in Linux with Examples" by Madhusudan_Soni, Geeks for Geeks is licensed under CC BY-SA 4.0
    "chgrp command in Linux with Examples" by Kaustav kumar Chanda, Geeks for Geeks is licensed under CC BY-SA 4.0


    03-B.3: Modify Ownership of Files and Directories is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?