In the previous blog, we covered Linux file and directory processing commands, which are still familiar. For beginners, we don’t need to remember the syntax of the commands in detail. Remember what the commands can do, and then look them up when we need them. This blog continues with Linux commands – link commands and permission management commands.
1. Link commands
Generate link file command: ln
① Command name: ln
②, English original meaning: link
3. The command path is /bin/link
④ Execute permission: all users
⑤, function description: generate link files
Ln -s [source file] [target file]
-s Creates a soft link
Create a hard link without adding -s
Soft: ln -s /etc/issuem/tmp/issue.soft
Hard: ln /etc/issue.tmp /issue.hard: ln /etc/issue.tmp /issue.hardWe can see:
First: soft links are preceded by l (link), while hard links are preceded by – (-), indicating files
Second, the soft link owner and owning group have all operation permissions, RWXRWXRWX; Hard links are not. That is, soft links are preceded by LRWXRWXRWX
Third: Soft links are similar to Windows shortcuts, with an obvious arrow pointing to the source file
Fourth: the hard link file name is different from the source file, all other information is the same. Similar to cp copy operation. But unlike replication, hard links can be updated synchronously.
Fifth: through the ls -i operation, to view the file I node. It is found that the hard link has the same I node as the source file, but the soft link has a different I node than the source file
Sixth: do not allow hard links to directories; Hard links across partitions are not allowed
2. Permission management commands
Change file or directory permission command: chmod
Command name: chmod
②, The permissions of a file
3. The command is stored in /bin/chmod
④ Execute permission: all users
⑤, function description: change file or directory permissions
Chmod {ugoa}{+-=}{RWX}】
【mode=421】【 file or directory 】
-r recursive modification
Note: Not every Linux user has permission to change a file or directory permission, and there are only two types of users who can
① The owner of the file. We view the details of a file by using the ls command to see who owns the file.
The root user is the user with the most privileges on the Linux system. Root can do things that others can’t.
For chmod [{ugoa}{+-=}{RWX}] [file or directory], we need to know that ugoa is: u: owner, g: group, O: other people, a: owner. And RWX stands for the following:For [mode=421] [file or directory], this is how we express the permissions in numbers, where r stands for 4, w stands for 2, x stands for 1, which is 2 to the 0, 1, and 2 respectively. The RWX permission number is 7, the RW-permission number is 6, and the r– permission number is 4.
Example 1: We grant permission to owner X of tmp.log in TMP; Grant permission W to the owning group and PERMISSION W to others.
chmod u+x /tmp/tmp.log
chmod g+w,o+w /tmp/tmp.logTo change the above example to use numbers, we want to grant tmp.log file permissions to rwxrw-rw-, which is 766. chmod 766 tmp.logWe can also recursively grant permissions by adding -r to all files or directories in a given directory.
Example 2: Grant permission 776 to all files and directories in TMP
chmod -R 776 /tmp
Change the file or directory owner command: chown
Command name: chown
②, English original meaning: change file ownership
3. The command is stored in /bin/chown
④ Execute permission: all users
⑤, function description: change the owner of a file or directory
⑥, syntax: chmod [user] [file or directory]
Note: The user who can change the owner of a file or directory is root
Here we create the user with the useradd [username] command and then enter the password with the passwd [username] command. We create vae users with these two commandsThen we changed the owner of tmp.log to vae user: chown vae tmp.log
To change the owning group of a file or directory, run the CHGRP command
Command name: CHGRP
②, English original meaning: change file group ownership
3. The command is stored in /bin/chown
④ Execute permission: all users
⑤, function description: change the owning group of a file or directory
⑥, syntax: CHGRP [user group] [file or directory]
Note: The user who can change the owner of a file or directory is root
Display and set the default file permission command: umask
Command name: umask
②, English original meaning: the user file-creation mask
3. Command path: shell built-in command
④ Execute permission: all users
⑤, function description: display and set the default permissions of files
⑥, grammar: umask [-s]
-s Displays the default permissions of new files in the format of RWX
Note: if you don’t understand the meaning of this command, we will execute umask and umask-s respectively, as follows:The umask execution displays 0022, with the first 0 indicating special permissions, which we will explain separately later. 022 represents the permission mask value. We subtract 0, 2, 2 from 7, 7 to 755, which represents the following rwxr-xr-x output by adding -s, which is 755 in numbers.
The default permission owner for creating a file is RWX, the group to which the file belongs is Rx, and others are Rx. Create a new file with the default permission of rwxr-xr-x.We found that we created a file a.txt using the touch command, and found that the permissions were not rwxr-xr-x, but rw-r–r–. The comparison shows that there are three x’s missing, which means there are fewer executable permissions. Why is that?
This is because in Linux, all newly created files do not have executable permissions. This is a way for Linux to protect itself, because similar virus trojans have executable permissions. So on Linux, newly created files do not have executable permissions.
So how do we set the default permissions? For example, we want to set the permissions of the newly created file to rwxr-xr–, that is, 754. We subtract 754 from 777 to get 023. This is done by executing umask 023 to set the default permissions.
3, summarize
In this article we looked at the link command ln and the permission management command. First of all, for the link command, we should pay attention to the difference between the soft link and the hard link. The soft link is similar to the Shortcut of Windows, with an obvious arrow pointing to the source file. Hard links we can imagine as cp -p + synchronous update, that is to say, the hard link created retains the same properties as the original file, and the original file changes, the hard link will also change.
Chmod 777 (file or directory) : chmod 777 (file or directory) : chmod 777 (file or directory) : chmod The chown command can change the owner of a file or directory, and the CHGRP command can change the owning group of a file or directory. Note that the latter two commands can only be done by root, whereas chmod can be done by root and also by the owner of the file or directory being changed.