In Lesson 20, “File I/O and OS Operations,” we covered the use of files. A part of that use involved setting file permissions. In this appendix, we provide additional information about file permissions and access rights.
In general, each file has associated permissions that define how a user can interact with the file. In a Unix-like OS such as Linux, for example, there are three different access rights for files:
The access rights for each file are defined for three categories of users:
In Linux, you can display the access right for each user by using the ls
command. Use the ls
command with the -l
option to display user access rights to each object in the current directory. To see how this works, Figure A.1 shows an example of a Linux file named file.txt
in the root directory. This shows the result of entering the following command at the root command line:
ls -l
To the left of the filename in Figure A.1, you will see the file permissions defined as follows:
-rw-rw-r--
The first dash refers to a file type. A plain file is represented by a -
(hyphen) and directories are represented by d
. The character c
is used for character-device files, b
is used for block-device files, s
is for a local socket, p
represents a named pipe, and l
represents a symbolic link.
The –rw-rw-r--
values represent read, write, execute for the three types of users mentioned earlier:
-rw- rw- r--
uuu ggg ooo
r
and w
, and the dash indicates that the user does not have execute rights.r
and w
, and does not have execute rights, indicated by the final dash.In Linux, the chmod
command can be used to change the access permission for files and directories. This command can be used to remove all access rights to other users by providing the empty access right for o
(others), as evidenced by the change from -rwxr--r--
to -rwxr-----
. With this change, file.txt
can no longer be accessed by other users.
You can also combine access rights for different types of users. For example, the following command can be entered for the results shown in Figure A.2:
chmod og=r file.txt
In this example, we assigned read access rights to the group and to other users. Note that the permission settings on file.txt
change from -rwxr-----
to –rwxr--r--
.
You can use a digit representation of the permissions using the values shown in Table A.1. For instance, the permissions –rw-rw-r--
of the text file can be represented as 664:
rw-
)rw-
)r--
)Table A.1: Permission representations
Number | Permission | Symbol |
---|---|---|
0 | No permission | --- |
1 | Execute | --x |
2 | Write | -w- |
3 | Execute+Write | -wx |
4 | Read | r-- |
5 | Read+Execute | r-x |
6 | Read+Write | rw- |
7 | Read+Write+Execute | rwx |