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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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