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
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 # 创建用户
In the above command, the -m
option is used to create the user's home directory.
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 # 删除用户,同时删除用户的家目录
In the above command, the -r
option is used to delete the user's home directory at the same time.
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 # 修改用户名
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. chmod
You can use numbers or symbols to represent permissions.
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
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:
0
: No permission1
: Execute permission2
: Write permission3
: Write and execute permission4
: Read permission5
: Read and execute permissions6
: Read and write permissions7
: Read, write and execute permissionsUse 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
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.
Create a user and set a password
sudo useradd -m testuser # 创建用户 sudo passwd testuser # 设置用户密码
Modify the user’s home directory permissions
sudo chmod 700 /home/testuser # 设置用户的家目录权限为rwx------
Create Create a new directory and give it to the user
mkdir /data/testdir # 创建新目录 sudo chown testuser:testuser /data/testdir # 将目录赋予给用户
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 # 将文件赋予给用户
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!