This article will introduce basic user group and file permission concepts and operation commands in Linux. Wish can help you in code development.
1. User Group.
- Each user in Linux must belong to a group and cannot be independent of the group.
- Each file in Linux has the concept of owner, group, and other groups.
- Owner: The file owner is typically the creator of the file. Who created the file, who is naturally the owner of the file. You can see the owner of the file with the “ ls ‐ahl ” command. You can also use the “ chown username filename” to modify the file owner.
- Group: When a user creates a file, the user belongs group is assigned to the file belongs group automatically. You can see file belongs groups with the ” ls ‐ahl ” command. You can also use the ” chgrp groupname filename ” to modify the group that the file belongs.
- Other Groups: Other users of the system are other groups of the file except for the owner of the file and the users in the file belonged group.
1.1 Change Owner (chown) And User Group (chgrp) Commands Example.
- Change the owner of test.txt to richard.
chown richard test.txt
- Change ./document directory’s owner to root.
chown root ./document
- Change directory ./document and all it’s child files or directory’s owner to jerry recursively.
chown ‐R jerry ./document
- Change the group of test.txt to dev.
chgrp dev test.txt
1.2 Change User Belongs Group
- You can specify a user group when you add a user, you can also modify the user group with root administrative privileges after you add that user. The below command example is used to add group, user, and set the user password.
# add group1 and group2 groupadd group1 groupadd group2 # add user jerry in group1 and richard in group2 useradd -g group1 jerry useradd -g group2 richard # set jerry and richard's password. password jerry password richard # change jerry's group to group2. usermod -g group2 jerry # change user login home directory usermod -d /usr/root jerry
2. File Permissions
- When you run the ” ls -l ” command in Linux, you can get results like below.
-rwxrw-r‐- 3 jerry root 1K Jan 19 19:29 test.txt drwxrw-r‐- 6 jerry root 12K Jan 19 19:29 doc lrwxrw-r‐- 9 root root 5k Jan 19 19:29 test.txt
- -rwxrw-r–: These 10 characters determine what different users can do with the file.
- The first character represents the type of this file. It can be file (-), directory (d), or link (l).
- The remaining characters are divided into three groups, each group has three characters (RWX), read (r), write (w), and execute (x).
- The first group represents file owner permissions. In this example, it is rwx which means the file owner has read, write and execute permissions to this file.
- The second group represents file group users ( file owner’s same group users ) permissions. In this example, it is rw- which means file group users have read, write permission but can not execute this file.
- The third group represents other group users’ permission to this file. In this example, it is r– which means they have only read permission to this file.
- We can also use a number to represent read, write and execute permission. read = 4, write = 2, execute = 1. So rwx can be represented by 4+2+1 = 7.
- Below is an example.
-rwxrw-r-- 3 jerry root 1K Jan 19 19:29 test.txt 3 : represent linked file number, that means there are 3 other link type file reference to this file. jerry : is the user name. root : is jerry belongs group name. 1K : is the file size. Jan 19 19:29: is the last file modify time. test.txt: is the file name.
2.1 Change File Permission Commands.
- chmod is used to change the permissions of a file or directory.
- chmod 755 test.txt: give test.txt rwxr-xr-x permission.
- chmod u=rwx, g=rx, o=rx test.txt: u=user permission, g=group permissions, o=other group user permission.
- chmod u-x, g+w test.txt: remove test.txt file execute permission for the file owner, add test.txt write permission to group user.
- chmod a+r test.txt: add test.txt read permissions to all users.