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

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.

  1. Owner:
    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.
  2. Group:
    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.
    linux ls ahl command output
  3. 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.

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

1.2 Change User Belongs Group

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

READ :   How To Setup ShadowSocks VPN On Mac, Windows And Linux

linux ls -l command result list explanation

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

  1. The first character represents type of this file. It can be file (-), directory (d) or link (l).
  2. The remaining characters are divided into three groups, each group has three characters (RWX), read (r), write (w) and execute (x).
  3. 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.
  4. 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.
  5. 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.
  6. 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.

  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 file owner, add test.txt write permission to group user.
  5. chmod a+r test.txt : add test.txt read permissions to all users.
(Visited 155 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

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