In Linux systems, permission management is a very important task. By correctly configuring the permissions of files and directories, you can ensure the security and reliability of your system. This article will introduce some commonly used Linux permission commands to help you better understand and manage permissions.
1. ls -l
ls -l
The command is used to display detailed information about files and directories, including permissions, owners, groups, etc. After executing the command, information similar to the following format will be output:
-rw-r--r-- 1 user group 4096 Aug 20 10:30 example.txt
drwxr-xr-x 2 user group 4096 Aug 20 10:30 directory
In the above example, the first column shows the permissions of the file or directory. Take -rw-r--r--
for example , the first character indicates the file type, -
which indicates an ordinary file and d
indicates a directory. The next three characters rw-
represent the owner's permissions, r--
which represent the group's permissions, and the last three characters r--
represent other users' permissions.
2. chmod
chmod
The command is used to modify the permissions of a file or directory. It specifies the granting or withdrawal of permissions by using different permission tags. Here are some common chmod
command examples:
-
Add executable permissions to the file owner:
chmod u+x file
-
Remove write permissions from the group to which the file belongs:
chmod g-w file
-
Remove read, write, and execute permissions from other users:
chmod o-rwx file
3. chown
chown
The command is used to modify the owner and group of a file or directory. You can use the following command to change the owner of the file to the specified user and the group to the specified group:
chown user:group file
For example, to example.txt
change the owner of a file to john
and the group it belongs to users
, you can execute the following command:
chown john:users example.txt
4. chgrp
chgrp
The command is used to modify the group to which a file or directory belongs. You can use the following command to change the group ownership of a file to a specified group:
chgrp group file
For example, to example.txt
change the group that a file belongs to users
, you can execute the following command:
chgrp users example.txt
5. umask
umask
Command is used to set the default permission mask for newly created files or directories. Permission masks determine the permissions of newly created files or directories. Here is an example:
umask 022
The above command sets the default permission mask to 022. This means that newly created files will have permissions 644 and newly created directories will have permissions 755.
6. su
su
The command is used to switch the user identity to another user. You can use the following command to switch the current user identity to a specified username:
su username
After executing this command, you need to enter the password of the target user to successfully switch to this user.
7. sudo
sudo
Command is used to execute the specified command as a super user. Use sudo
to temporarily gain administrator privileges to perform operations that require privileges, such as installing software or modifying system files.
sudo command
For example, to execute the command as superuser apt-get update
to update the package list, you can execute the following command:
sudo apt-get update
Please note that sudo
the current user's password is required when using the command.
8. passwd
passwd
Command is used to change user password. You can use the following command to change the password of a specified user:
passwd username
After executing this command, you will be prompted to enter a new password and confirm it.
9. adduser
adduser
Command is used to create a new user account. Execute the following command to create username
a new user account named:
adduser username
After executing the command, you will be asked to enter the new user's password and some other information.
10. deluser
deluser
Command is used to delete user accounts. The following command will delete username
the user account named :
deluser username
When executing this command, the system will ask whether to delete files and directories associated with the user at the same time.
The above are some commonly used Linux permission commands. By learning and mastering these commands, you can better manage and protect your system's files and directories.