To change file or directory permissions, you use the chmod (change mode) command. There are two ways to use chmod: symbolic mode and absolute mode.
Using chmod in Symbolic Mode:
The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.
| Chmod operator | Description |
| + | Adds the designated permission(s) to a file or directory. |
| - | Removes the designated permission(s) from a file or directory. |
| = | Sets the designated permission(s). |
Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as follows:
| [amrood]$ls -l testfile -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile |
Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes:
| [amrood]$chmod o+wx testfile [amrood]$ls -l testfile -rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile [amrood]$chmod u-x testfile [amrood]$ls -l testfile -rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile [amrood]$chmod g=r-x testfile [amrood]$ls -l testfile -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile |
Here's how you could combine these commands on a single line:
| [amrood]$chmod o+wx,u-x,g=r-x testfile [amrood]$ls -l testfile -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile |
Using chmod with Absolute Permissions: (1,2,4)
The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.
| Number | Octal Permission Representation | Ref |
| 0 | No permission | --- |
| 1 | Execute permission | --x |
| 2 | Write permission | -w- |
| 3 | Execute and write permission: 1 (execute) + 2 (write) = 3 | -wx |
| 4 | Read permission | r-- |
| 5 | Read and execute permission: 4 (read) + 1 (execute) = 5 | r-x |
| 6 | Read and write permission: 4 (read) + 2 (write) = 6 | rw- |
| 7 | All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 | rwx |
Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as follows:
| [amrood]$ls -l testfile -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile |
Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes:
| [amrood]$ chmod 755 testfile [amrood]$ls -l testfile -rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile [amrood]$chmod 743 testfile [amrood]$ls -l testfile -rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile [amrood]$chmod 043 testfile [amrood]$ls -l testfile ----r---wx 1 amrood users 1024 Nov 2 00:10 testfile |
No comments:
Post a Comment