Chapter 17
Chapter 17
chown
ls -a
ls -l
17.1 Introduction
File ownership is critical for file security. Every file has a user owner and a group owner.
This chapter focuses on how to specify the user and group ownership of a file. In addition, the
concept of file and directory permissions is explored, including how to change the permissions on
files and directories. Default permissions are the permissions given to files and directories when
they are initially created.
Previous
Next
sysadmin@localhost:~$ id
uid=1001(sysadmin) gid=1001(sysadmin)
groups=1001(sysadmin),4(adm),27(sudo),1005(research),1006(development)
The above example shows the user has a UID of 1001 for the user account sysadmin. It also
shows that the primary group for this user has a GID of 1001 for the group sysadmin.
Because the user account and primary group account have the same numeric identifier and
name, this indicates that this user is in a User Private Group (UPG). In addition, the user in this
example belongs to four supplemental groups: the adm group with a GID of 4, the sudo group
with a GID of 27, the research group with a GID of 1005 and the development group with a
GID of 1006.
When a file is created, it belongs to the current user and their current primary group. If the user
from the previous example executes the touch command to create a file, then the user owner of
the file is the sysadmin user, and the group owner is the sysadmin group:
sysadmin@localhost:~$ touch /tmp/filetest1
The file ownership can be confirmed using the long listing -l option of the ls command.
sysadmin@localhost:~$ ls -l /tmp/filetest1
-rw-rw-r--. 1 sysadmin sysadmin 0 Oct 21 10:18 /tmp/filetest1
File ownership also applies to hidden files in the system. Hidden files, which begin with the
period . character are listed using the -a option of the ls command. The first two hidden files
listed are the current . and parent .. directories respectively. The ownership of all files and
subdirectories within the current directory can be listed using the ls -la command.
sysadmin@localhost:~$ ls -la
total 60
drwxr-xr-x 1 sysadmin sysadmin 4096 Nov 3 22:29 .
drwxr-xr-x 1 root root 4096 Mar 14 2016 ..
-rw-r--r-- 1 sysadmin sysadmin 220 Apr 3 2012 .bash_logout
-rw-r--r-- 1 sysadmin sysadmin 3768 Mar 14 2016 .bashrc
drwx------ 2 sysadmin sysadmin 4096 Nov 3 22:29 .cache
-rw-r--r-- 1 sysadmin sysadmin 675 Apr 3 2012 .profile
-rw-r--r-- 1 sysadmin sysadmin 74 Mar 14 2016 .selected_editor
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Desktop
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Downloads
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 14 2016 Videos
Consider This
The output of the ls -l command includes multiple pieces of information that are relevant to this
chapter including:
Permissions
User Owner
Group Owner
Previous
Next
newgrp group_name
The id command lists your identity information, including your group memberships. If you are
only interested in knowing what groups you belong to, then you can execute
the groups command:
sysadmin@localhost:~$ groups
sysadmin adm sudo research development
The output of the groups command may not be as detailed as the output of the id command, but
if all you need to know is what groups you can switch to by using the newgrp command, then
the groups command provides the information that you need. The id command output does
show your current primary group, so it is useful for verifying that the newgrp command
succeeded.
For example, if the sysadmin user was planning on having a file owned by the
group research, but that wasn't the user's primary group, then the user could use
the newgrp command and then verify the correct primary group with the id command before
creating the new file:
sysadmin@localhost:~$ id
uid=1001(sysadmin) gid=1001(sysadmin)
groups=1001(sysadmin),4(adm),27(sudo),1005(research),1006(development)
sysadmin@localhost:~$ newgrp research
sysadmin@localhost:~$ id
uid=1001(sysadmin) gid=1005(research)
groups=1005(research),4(adm),27(sudo),1001(sysadmin),1006(development)
According to the output of the previous commands, initially the user's GID is 1001 for the
sysadmin user, then the newgrp command is executed, and the user's primary GID
becomes 1005, the research group. After these commands were executed, if the user were to
create another file and view its details, the new file would be owned by the research group:
The newgrp command opens a new shell; as long as the user stays in that shell, the primary
group won't change. To switch the primary group back to the original, the user can leave the new
shell by running the exit command. For example:
sysadmin@localhost:~$ id
uid=1001(sysadmin) gid=1005(research)
groups=1005(research),4(adm),27(sudo),1001
(sysadmin),1006(development)
sysadmin@localhost:~$ exit
exit
sysadmin@localhost:~$ id
uid=1001(sysadmin) gid=1001(sysadmin)
groups=1001(sysadmin),4(adm),27(sudo),1005(research),1006(development)
Consider This
Administrative privileges are required to change the primary group of the user permanently. The
root user would execute the following command:
usermod -g groupname username
Previous
Next
As the root user, the chgrp command can be used to change the group owner of any file to any
group. As a user without administrative privileges, the chgrp command can only be used to
change the group owner of a file to a group that the user is already a member of:
-rw-rw-r-- 1 sysadmin sysadmin 0 Oct 23 22:12 sample
sysadmin@localhost:~$ chgrp research sample
sysadmin@localhost:~$ ls -l sample
-rw-rw-r--. 1 sysadmin research 0 Oct 23 22:12 sample
If a user attempts to modify the group ownership of a file that the user doesn't own, they receive
an error message:
To change the group ownership of all of the files of a directory structure, use the recursive -
R option to the chgrp command. For example, the command in the following example would
change the group ownership of the test_dir directory and all files and subdirectories of
the test_dir directory.
Consider This
While you can view the ownership of a file with the -l option to the ls command, the system
provides another command that is useful when viewing ownership and file permissions:
the stat command. The stat command displays more detailed information about a file,
including providing the group ownership both by group name and GID number:
sysadmin@localhost:~$ stat /tmp/filetest1
File: `/tmp/filetest1'
Size: 0 Blocks: 0 IO Block: 4096 regular empty
file
Device: fd00h/64768d Inode: 31477 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/sysadmin) Gid: ( 1001/sysadmin)
Access: 2013-10-21 10:18:02.809118163 -0700
Modify: 2013-10-21 10:18:02.809118163 -0700
Change: 2013-10-21 10:18:02.809118163 -0700
Previous
Next
For example, if the root user wanted to change the user ownership of the abc.txt file to the
user jane, then the following command could be executed:
The second method is to change both the user and the group; this also requires root privileges.
To accomplish this, you separate the user and group by either a colon or a period character. For
example:
If a user doesn't have root privileges, they can use the third method to change the group owner
of a file just like the chgrp command. To use chown only to change the group ownership of the
file, use a colon or a period as a prefix to the group name:
Next
17.6 Permissions
The output of the ls -l command displays ten characters at the beginning of each line. These
characters indicate the type of file and the permissions of the file. For example, consider the
output of the following command:
root@localhost:~# ls -l /etc/passwd
-rw-r--r--. 1 root root 4135 May 27 21:08 /etc/passwd
File Type
The first character of each line indicates the type of file:
The following table describes the possible values for the file type:
Characte
r Type of the File
d A directory file, which contains the names of other files and links to them.
b A block file is one that relates to a block hardware device where data is read in
blocks of data.
c A character file is one that relates to a character hardware device where data is read
one byte at a time.
p A pipe file works similar to the pipe symbol, allowing for the output of one process
to communicate to another process through the pipe file, where the output of the
one process is used as input for the other process.
Consider This
Although all the file types are listed in the table above, typically you don’t encounter anything but
regular, directory and link files unless you explore the /dev directory.
Permission Groups
The next nine characters demonstrate the permissions of the file.
The permissions set on these files determine the level of access that a user has on the file.
When a user runs a program and the program accesses a file, then the permissions are checked
to determine whether the user has the correct access rights to the file.
The permissions are grouped into three different roles, representing the different users that may
try to access the file.
If you aren't the owner and you're not a member of the file/directory group, then your permissions
would be others.
User Owner
Characters 2-4 indicate the permissions for the user that owns the file. If you are the owner of the
file, then only the user owner permissions are used to determine access to that file.
Group Owner
Characters 5-7 indicate the permissions for the group that owns the file. If you are not the owner
but are a member of the group that owns the file, then only group owner permissions are used to
determine access to that file.
Other Permissions
Permission Types
Each group is attributed three basic types of permissions: read, write, and execute.
On a file, this allows processes to read the contents of the file, meaning the contents can
be viewed and copied.
On a directory, file names in the directory can be listed, but other details are not
available.
Write
The second character of each group represents the write permission. There is a w character if
the group has the write permission, or a - character if the group does not.
Execute
The third character of each group represents the execute permission. There is an x character if
the group has the execute permission, or a - character if the group does not.
Previous
Next
The relevant information is highlighted. The first line represents the / directory, with a user owner
of root, a group owner of root and permissions of rwxr-xr-x. The second line represents
the /data directory, a directory that is under the / directory. The third line represents
the abc.txt file, which is stored in the /data directory.
Previous
Next
Previous
Next
Answer: None.
Explanation: Initially it would appear that the user bob can view the contents of
the abc.txt file as well as copy the file, modify its contents and run it like a program. This
erroneous conclusion would be the result of looking solely at the file's permissions (rwx for the
user bob in this case).
However, to do anything with the file, the user must first "get into" the /data directory. The
permissions for bob for the /data directory are the permissions for "others" (r--), which
means bob can't even use the cd command to get into the directory. If the execute permission
(--x) were set for the directory, then the user bob would be able to "get into" the directory,
meaning the permissions of the file itself would apply.
Lesson Learned: The permissions of all parent directories must be considered before
considering the permissions on a specific file.
Previous
Next
Previous
Next
Answer: All users.
Explanation: All that is required to be able to view a directory's contents is r permission on the
directory (and the ability to access the parent directories). The x permission for all users in
the / directory means all users can use / as part of a path, so everyone can get through
the / directory to get to the /data directory. The r permission for all users in the /data directory
means all users can use the ls command to view the contents. This includes hidden files, so
the ls -a command also works on this directory.
However, note that in order to see file details (ls -l), the directory would also
require x permission. So while the root user and members of the root group have this access
on the /data directory, no other users would be able to execute ls -l /data.
Lesson Learned: The r permission allows a user to view a listing of the directory.
Previous
Next
Next
Answer: Only the root user.
Explanation: A user needs no permissions at all on the file itself to delete a file.
The w permission on the directory that the file is stored in is required to delete a file in a directory.
Based on that, it would seem that all users could delete the /data/abc.txt file, since
everyone has w permission on the directory.
However, to delete a file, you must also be able to "get into" the directory. Since only
the root user has x permission on the /data directory, only root can "get into" that directory to
delete files in this directory.
Lesson Learned: The w permission allows a user to delete files from a directory, but only if the
user also has x permission on the directory.
Previous
Next
Previous
Next
Answer: True.
Explanation: As previously mentioned, to access a file, the user must have access to the
directory. The access to the directory only requires x permission; even though r permission
would be useful to list files in a directory, it isn't required to "get into" the directory and access
files within the directory.
When the command more /data/abc.txt is executed, the following permissions are
checked: x permission on the / directory, x permission on the data directory and r permission
on the abc.txt file. Since the user bob has all of these permissions, the command executes
successfully.
Lesson Learned: The x permission is required to "get into" a directory, but the r permission on
the directory is not necessary unless you want to list the directory's contents.
Previous
Next
Previous
Next
Answer: Not enough information to determine.
Explanation: In order to access the /data/abc.txt file, the user bob needs to be able to "get
into" the /data directory. This requires x permission, which bob may or may not have,
depending on whether he is a member of the payroll group.
If bob is a member of the payroll group, then his permissions on the /data directory are r-x,
and the command more will execute successfully (bob also needs x on / and r on abc.txt,
which he already has).
If he isn't a member of the payroll group, his permissions on the /data directory are ---, and
the more command will fail.
Lesson Learned: You must look at each file and directory permissions separately and be aware
of which groups the user account belongs to.
Previous
Next
Previous
Next
Answer: False.
Explanation: Recall that if you are the owner of a file, then the only permissions that are
checked are the user owner permissions. In this case, that would be --- for bob on
the /data/abc.txt file.
In this case, members of the bob group and "others" have more permissions on the file
than bob has.
Lesson Learned: Don't provide permissions to the group owner and "others" without applying at
least the same level of access to the owner of the file.
Previous
Next
Important: To change a file's permissions, you either need to own the file or log in as the root
user.
The following examples will use a sample file:
Previous
Next
g group owner
o others
- remove
= equals
w write
x execute
For example, to give the group owner write permission on a file named abc.txt, you could use
the following command:
Only the group owner's permission was changed. All other permissions remained as they were
prior to the execution of the chmod command.
You can combine values to make multiple changes to the file's permissions. For example,
consider the following command which adds the execute permission to the user owner and group
owner and removes the read permission for others:
Previous
Next
4 Read
2 Write
1 Execute
By using a combination of numbers from 0 to 7, any possible combination of read, write and
execute permissions can be specified for a single permission group set. For example:
7 rwx
6 rw-
5 r-x
4 r--
3 -wx
2 -w-
1 --x
0 ---
Consider This
Recall the stat command provides more detailed information than the ls -l command.
Because of this, you may consider using the stat command instead of the ls -l command
when viewing permissions on a file. One big advantage of the stat command is that it shows
permissions using both the symbolic and numeric methods, as highlighted below:
sysadmin@localhost:~$ stat /tmp/filetest1
File: `/tmp/filetest1'
Size: 0 Blocks: 0 IO Block: 4096 regular empty
file
Device: fd00h/64768d Inode: 31477 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 502/sysadmin) Gid: ( 503/sysadmin)
Access: 2013-10-21 10:18:02.809118163 -0700
Modify: 2013-10-21 10:18:02.809118163 -0700
Change: 2013-10-21 10:18:02.809118163 -0700
Previous
Next
file rw-rw-rw-
directories rwxrwxrwx
The permissions that are initially set on a file when it is created cannot exceed rw-rw-rw-. To
have the execute permission set on a file, you first need to create the file and then change the
permissions.
The umask command can be used to display the current umask value:
sysadmin@localhost:~$ umask
0002
Note that different users may have different umasks. Typically the root user has a more
restrictive umask than normal user accounts:
root@localhost:~# umask
0022
To understand how umask works, assume that the umask of a file is set to 027 and consider the
following:
Umask -027
Result 640
Because the default permissions for directories are different than for files, a umask of 027 would
result in different initial permissions on new directories:
Directory Default 777
Umask -027
Result 750
The new umask is only applied to file and directories created during that session. When a new
shell is started, the default umask will again be in effect.
Permanently changing a user's umask requires modifying the .bashrc file located in that user's
home directory.
Previous
Next