Home > Operation and Maintenance > Linux Operation and Maintenance > How to manage users and permissions in Linux systems

How to manage users and permissions in Linux systems

王林
Release: 2023-11-08 13:10:44
Original
1462 people have browsed it

How to manage users and permissions in Linux systems

How to manage users and permissions in Linux systems requires specific code examples

In Linux systems, user and permissions management is an important task, it can Helps system administrators control and protect access to system resources. This article will introduce how to manage users and permissions in Linux systems, including the creation, deletion and modification of users, as well as the setting and management of permissions. Also, for better understanding, at each step, specific code examples are provided.

1. User management

  1. Creating a user
    In the Linux system, you can use the useradd command to create a new user. The following is an example of creating a user named testuser:

    sudo useradd -m testuser  # 创建用户
    Copy after login

    In the above command, the -m option is used to create the user's home directory.

  2. Delete User
    If you want to delete a user, you can use the userdel command. The following is an example of deleting a user named testuser:

    sudo userdel -r testuser  # 删除用户,同时删除用户的家目录
    Copy after login

    In the above command, the -r option is used to delete the user's home directory at the same time.

  3. Modify user
    You can use the usermod command to modify the user's attributes. The following is an example of modifying a user named testuser to change its user name to newuser:

    sudo usermod -l newuser testuser  # 修改用户名
    Copy after login

    In the above command, -l option is used to modify the username.

2. Permission management
In the Linux system, you can use the chmod command to set and manage the permissions of files and directories. chmodYou can use numbers or symbols to represent permissions.

  1. Use numeric mode
    Use numeric mode to modify permissions, which can be achieved by assigning numbers to permission identifiers. The following is an example of setting the permissions of file test.txt to rwxr-xr-x:

    chmod 755 test.txt
    Copy after login

    In the above command, the number 755 Represents permissions, where the first number indicates the user's permissions, the second number indicates the group's permissions, and the third number indicates the permissions of other users. The specific meaning of each number is as follows:

  2. 0: No permission
  3. 1: Execute permission
  4. 2: Write permission
  5. 3: Write and execute permission
  6. 4: Read permission
  7. 5: Read and execute permissions
  8. 6: Read and write permissions
  9. 7: Read, write and execute permissions
  10. Use symbolic method
    Use symbolic method to modify permissions, which can be achieved by adding or removing permissions from the permission identifier. The following is an example of setting the permissions of the file test.txt to rwxr-xr-x:

    chmod u+rwx,g+rx,o+rx test.txt
    Copy after login

    In the above command, u means User permissions, g indicates group permissions, o indicates other user permissions, indicates adding permissions, - indicates removing permissions.

3. Permission Management Example
The following is a comprehensive example showing how to create users, modify user permissions and manage permissions.

  1. Create a user and set a password

    sudo useradd -m testuser  # 创建用户
    sudo passwd testuser  # 设置用户密码
    Copy after login
  2. Modify the user’s home directory permissions

    sudo chmod 700 /home/testuser  # 设置用户的家目录权限为rwx------
    Copy after login
  3. Create Create a new directory and give it to the user

    mkdir /data/testdir  # 创建新目录
    sudo chown testuser:testuser /data/testdir  # 将目录赋予给用户
    Copy after login
  4. Set the file permissions and grant the user read and write permissions. Other users only have read permissions

    touch /data/testdir/file.txt  # 创建一个文件
    sudo chmod 644 /data/testdir/file.txt  # 设置文件权限为rw-r--r--
    sudo chown testuser:testuser /data/testdir/file.txt  # 将文件赋予给用户
    Copy after login

Through these sample codes, you can learn how to manage users and permissions in Linux systems. By creating users, modifying user attributes, and setting permissions on files and directories, you can better control and protect access to system resources.

The above is the detailed content of How to manage users and permissions in Linux systems. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template