Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux File Basic Attributes

Linux File Basic Attributes

The Linux system is a typical multi-user system; different users have different statuses and different permissions.

To protect system security, Linux has different rules for the permissions of different users accessing the same file (including directory files).

In Linux, we usually use the following two commands to modify the owner and permissions of a file or directory:

In the figure below, chown is used to authorize a user, and chmod is used to set the permissions that allow the user to "open the door".

In Linux, we can use the ll or ls -l command to display a file's attributes and the user and group that own the file, for example:



[root@www /]# ls -l

total 64

dr-xr-xr-x   2 root root 4096 Dec 14  2012 bin

dr-xr-xr-x   4 root root 4096 Apr 19  2012 boot

……

In the example,bin the first attribute of the file is represented by d. In Linux, d indicates that the file is a directory file.

In Linux, the first character indicates whether the file is a directory, a regular file, a link file, and so on.

The following characters are grouped in threes, each being a combination of the three parameters rwx. Here, r stands for readable (read), w for writable (write), and x for executable (execute). Note that the positions of these three permissions do not change; if a permission is absent, a minus sign - simply appears.

The attributes of each file are determined by the 10 characters in the first part on the left (as shown below).

From left to right, use 0-9 these numbers to represent them.

The 0 bits determine the file type, bits 1-3 determine the permissions owned by the owner (the file's owner).

Bits 4-6 determine the permissions owned by the group (users in the owner's group), and bits 7-9 determine the permissions owned by other users.

Among them, the 1. 4. 7 bit indicates read permission; if represented by the character r, read permission is granted, and if represented by -, there is no read permission;

The 2. 5. 8 bit indicates write permission; if represented by the character w, write permission is granted, and if represented by -, there is no write permission; the 3. 6. 9 bit indicates execute permission; if represented by the character x, execute permission is granted, and if represented by -, there is no execute permission.


Linux File Owner and Group



[root@www /]# ls -l

total 64

drwxr-xr-x 2 root  root  4096 Feb 15 14:46 cron

drwxr-xr-x 3 mysql mysql 4096 Apr 21  2014 mysql

……

Every file has a specific owner, that is, the user who owns the file.

At the same time, in the Linux system, users are classified by groups, and a user belongs to one or more groups.

Users other than the file owner can be divided into users in the file's group and other users.

Therefore, the Linux system defines different file access permissions for the file owner, users in the owner's group, and other users.

In the above example, the mysql file is a directory file whose owner and group are both mysql; the owner has read, write, and execute permissions; other users in the owner's group have read and execute permissions; and other users also have read and execute permissions.

For the root user, in general, file permissions do not apply.


Change File Attributes

1. chgrp: Change the file's group

Syntax:



chgrp [-R] 属组名 文件名

Parameter options

2. chown: Change the file owner (owner), and can also change the owning group at the same time.

Syntax:



chown [–R] 所有者 文件名

chown [-R] 所有者:属组名 文件名

Enter the /root directory (~) and change the owner of install.log to the bin account:



[root@www ~] cd ~

[root@www ~]# chown bin install.log

[root@www ~]# ls -l

-rw-r--r--  1 bin  users 68495 Jun 25 08:53 install.log

Change the owner and group of install.log back to root:



[root@www ~]# chown root:root install.log

[root@www ~]# ls -l

-rw-r--r--  1 root root 68495 Jun 25 08:53 install.log

3. chmod: Change the 9 attributes of a file

There are two ways to set Linux file attributes: one is numeric, and the other is symbolic.

Linux has nine basic file permissions, namely owner/group/others (owner / group / others) Each of the three identities has its own read/write/execute permissions.

Let's review the data mentioned above: the permission characters of a file are -rwxrwxrwx, and these nine permissions are grouped in threes! We can use numbers to represent each permission; the score mapping table for each permission is as follows:

The scores of the three permissions (r/w/x) for each identity (owner/group/others) need to be added up. For example, when the permission is -rwxrwx---, the score is:

So when we set the permission change later, the file's permission number is 770. The syntax of the chmod command for changing permissions is as follows:

 chmod [-R] xyz 文件或目录

Options and parameters:

For example, if you want to set .bashrc all permissions of this file to be enabled, the command is as follows:



[root@www ~]# ls -al .bashrc

-rw-r--r--  1 root root 395 Jul  4 11:45 .bashrc

[root@www ~]# chmod 777 .bashrc

[root@www ~]# ls -al .bashrc

-rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc

What if you want to change the permissions to -rwxr-xr-- ? Then the permission score becomes [4+2+1][4+0+1][4+0+0]=754.

Symbolic-type change of file permissions

There is another way to change permissions. From the earlier introduction, we can see that there are basically nine permissions, namely:

Then we can use u, g, o to represent the permissions of the three identities.

In addition, a represents all, that is, all identities. Read and write permissions can be written as r, w, x, which can be viewed as in the following table:

| chmod | u
g
o
a | +(add)
-(remove)
=(set) | r
w
x | file or directory | | --- | --- | --- | --- | --- |

If we need to set file permissions to -rwxr-xr-- , we can use chmod u=rwx,g=rx,o=r filename to set it:



#  touch test1    // create test1 file

# ls -al test1    // View default permissions of test1

-rw-r--r-- 1 root root 0 Nov 15 10:32 test1

# chmod u=rwx,g=rx,o=r  test1    // Modify test1 permissions

# ls -al test1

-rwxr-xr-- 1 root root 0 Nov 15 10:32 test1

And what if you want to remove a permission without changing other existing permissions? For example, to remove execute permission for everyone, use:



#  chmod  a-x test1

# ls -al test1

-rw-r--r-- 1 root root 0 Nov 15 10:32 test1


More reference material