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.
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 “ ls ‐ahl ” command. You can also use the “ chown username filename” to modify the file owner.
When a user creates a file, the user belongs group is assigned to file belongs group automatically. You can see file belongs groups with ” 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 the owner of the file and the users in file belongs group.
1.1 Change Owner (chown) And User Group (chgrp) Commands.
- chown richard test.txt : change the owner of test.txt to richard.
- chown root ./document : change ./document directory’s owner to root.
- chown ‐R jerry ./document : Change directory ./document and all it’s child file or directory’s owner to jerry recursively.
- chgrp dev test.txt : change the group of test.txt to dev.
1.2 Change User Belongs Group
- You can specify user group when you add a user, you can also modify user group with root administrative privileges after you add that user. Below are commands to add group, user and set 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 ” 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– : This 10 characters determines what different users can do with the file.
- The first character represents 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 represent 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 represent file group users ( file owner’s same group users ) permissions. In this example it is rw- which means file group users has read, write permission but can not execute this file.
- The third group represent other group users permission to this file. In this example it is r– which means they has 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.
-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 file owner, add test.txt write permission to group user.
- chmod a+r test.txt : add test.txt read permissions to all users.