Linux User Group And File Permission Introduction

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.

  1. Each user in Linux must belong to a group and cannot be independent of the group.
  2. Each file in Linux has the concept of owner, group, and other groups.
  3. 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.
  4. 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.
  5. 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.

  1. Change the owner of test.txt to richard.
    chown richard test.txt
  2. Change ./document directory’s owner to root.
    chown root ./document
  3. Change directory ./document and all it’s child files or directory’s owner to jerry recursively.
    chown ‐R jerry ./document
  4. Change the group of test.txt to dev.
    chgrp dev test.txt

1.2 Change User Belongs Group

  1. 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

  1. 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
  2. -rwxrw-r–: These 10 characters determine what different users can do with the file.
  3. The first character represents the type of this file. It can be file (-), directory (d), or link (l).
  4. The remaining characters are divided into three groups, each group has three characters (RWX), read (r), write (w), and execute (x).
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.

  1. chmod is used to change the permissions of a file or directory.
  2. chmod 755 test.txt: give test.txt rwxr-xr-x permission.
  3. chmod u=rwx, g=rx, o=rx test.txt: u=user permission, g=group permissions, o=other group user permission.
  4. 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.
  5. chmod a+r test.txt: add test.txt read permissions to all users.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x