Beginners Learn Linux-set permissions for files/folders

Source: Internet
Author: User

In Linux, we can set permissions on files or folders (r,w,x,-). However, the permission settings for files and folders have different meanings. Here are a few examples of what permissions mean. Before we get started, we need to know a few commands to modify permissions:

Note: In a test environment, it is demonstrated by two sets of user names/user groups. The two groups were: Root/root,jonathan/jonathan. In addition, the following example is continuous, that is, the current example is based on the previous example.

chgrp -Change group, which is used to modify the file/folder user groups.

Now,/home/jonathan/jonathantestfolder there is a JonathanFile.txt, first look at the properties of this file, listed by Ls-l:

Below, through CHGRP, the user group is changed to root, that is:chgrp Root JonathanFile.txt

chown -Change ownership, use this command to alter the file/folder owner. In fact, you can also use this command to achieve the function of CHGRP, following a few examples to illustrate:

Change the owner of JonathanFile.txt to Root:chown root JonathanFile.txt

Change JonathanFile.txt's owner/group to Jonathan/jonathan:chown Jonathan:jonathan JonathanFile.txt

Change JonathanFile.txt Group to root:chown : Root JonathanFile.txt [In fact, the colon here (:) is also available with dots (.) Instead, but when we start the user name, we may also use the arrival point, so it is unified with the colon bar]

chmod -Change mode, use this command to alter the permissions on the file/folder (that is, rwx-). There are several ways to change permissions:

1. The rwx-corresponds to a different value, i.e. r = 4,w = 2,x = 1,-= 0. So, rwx = 4 + 2 +1 = 7,rw-= 4 + 2 + 0 = 6. Then, the string representing the permission can be represented by the corresponding number combination. For example, now JonathanFile.txt's permission is:-rw-rw-r--, then the numerical representation is:-664. Now, with this command, we change this permission for this file:-rwxrwx---。 So, we can do: chmod 770 JonathanFile.txt (in the command, the first character, that is, the character that represents the file type is not included)

2. We can also use RWX directly to set permissions. In this way, you need to display the rights to make the user/user group available. The user/user group is represented by Ugoa in the command. U represents the file that belongs to, that is owner. And so on: G represents the user group, O represents others. A means all, all. In the last example, we have changed the permissions to RWX rwx---, now we want the owner's permissions to remain rwx, but the user group to change the permissions to R--,others to r--, then we can do:chmod u=rwx,go=r-- JonathanFile.txt

3. If we do not care about the current permissions, we just want all users (all) to have X permission, then we can execute:chmod a+x JonathanFile.txt (+ means to add some kind of permission, corresponding,-means to remove a certain kind of permission)

So far, I believe we have a certain understanding of the chgrp/chown/chmod. In fact, there are a lot of options can be combined with these commands, using the process, more than man bar. So, what is the meaning of permissions for files/folders?

The meaning of permissions for a file: For a file, permissions determine the degree to which a user can manipulate a file's content: readable, writable, or executable. It is important to note that, in Linux, the file executable is not controlled by the file's extension, but rather whether it has X permissions. We may encounter files downloaded on the Internet is not executable, then this situation can first check whether the file has X permission. /

The meaning of permissions for a folder: In fact, the file price can be viewed as a list, which includes all files/folders below the file price. For folders, R and W are good to understand, so what about X? What does x mean? Let's go straight to a few examples:

1. We modify the Jonathantestfolder permissions so that user Jonathan has only RW permissions, but no x. Then, LS, we can only know that there is a file under the directory, but the details are not visible.

2. Then the previous example, there is no X, only RW, I cannot view the folder below the details of the file, then I can create a new file under this folder. The answer is no, because I have no way to get into this folder, because there is no X

From the above example, it is important to get X for the folder. Even with RW but no X, we have no way to access the files under the folder.

So, does that mean that the X permission on the folder should be given? Of course not, when the folder has W and x permissions, we can delete any file under the folder, even if we do not have any permissions on the following files. Let's look at an example:

1. We revise Jonathan's authority over Jonathantestfolder to make it rwx

2. Use the root account to create a file rootfile.txt under Jonathantestfolder and set permissions so that Jonathan has no permissions on this file

3. Use Jonathan account to delete Rootfile.txt success!

So, set permissions, must be cautious Oh! (End of this article)

Beginners Learn Linux-set permissions for files/folders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.