Latest web development tutorials

Linux basic file attributes

Linux system is a typical multi-user systems, different users in a different position, with different privileges. To protect the security of the system, Linux system to access the same files (including the catalog file) permissions for different users to do different provisions.

In Linux, we can use the ll or ls -l command to display user and group attributes of a file and the file belongs to, such as:

[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
……

Example, the first attribute bin file with the "d" represents. "D" represents Linux, the file is a directory file.

In Linux, the first character represents the file is a directory, the file or linked files, and so on.

  • When the [d] is the directory
  • When is [-] is the file;
  • If [l] is represented as a linked document (link file);
  • If [b] indicates that the interface device is a device for storage of files inside (random access devices);
  • If the [c] is expressed as a file inside the device serial port device, such as a keyboard, a mouse (one reading device).

The next character in groups of three, and are a combination of "rwx" of the three parameters. Wherein, [r] Representative readable (read), [w] representative may write (write), [x] on behalf of the executable (execute). It should be noted that the position of these three rights will not change, if there is no authority, there will be a minus sign [-] only.

Each file's properties by the left of the first part of the 10 characters is determined (see below).

363003_1227493859FdXT

0-9 from left to right with these numerals.

Bit 0 determines the file type, bits 1-3 determine the owner (owner of the file) have access to the file.

The first is a group of 4-6 OK (owner of the same group of users) have access to the file, 7-9 bits determine other users with access to the file.

Wherein the first 1,4,7 indicates read permission, if "r" character representation, the read access, if the "-" character, there is no read permission;

The first 2,5,8 indicates write permission, if the "w" character representation, you have write access, if the "-" character is not writable; 3,6,9 indicates the first executable permissions, if " x "character representation, the Executive authority, if the" - "character, no execute permissions.


Linux file owner and group

[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
……

For a file, which has a particular owner, i.e. the user has ownership of the document.

Meanwhile, in the Linux system, users are classified into groups, and a user belongs to one or more groups.

Users other than the owner of the file can be divided into the same group of users of the file owner and other users.

Therefore, Linux system by file owner, file owner the same group and other users to specify a different file access rights.

In the above example, bin file is a directory file, the owner and group are root, the owner has read, write, execute permissions; the same group with the owner of the other users have read and enforceable authority; other users have read and executable permissions.

Change the file attributes

1, chgrp: Change the document is set

grammar:

chgrp [-R] 属组名文件名

Parameter options

  • -R: Recursively change the file belongs to the group, that is, when you change a file's directory belong to the group, if coupled with -R parameter, then the case will be set to change all the files in the directory.

2, chown: Change file owner, you can also change the file belongs to the group at the same time

grammar:

chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名

Enter the / root directory (~) will install.log owner to bin this 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

The install.log owner and group changed 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 file attributes 9

Linux file attributes set in two ways, one is digital, one is the symbol.

Basic Linux file permissions have nine, are owner / group / others Three Identities have their own read / write / execute permissions.

To review the data just mentioned above: file permissions for the characters: "- rwxrwxrwx" nine permissions are three groups of three! Among them, we can use numbers to represent each permission, scores table for each permission as follows:

  • r: 4
  • w: 2
  • x: 1

Each identity (owner / group / others) the respective three permissions (r / w / x) cumulative score is required, for example when permissions: [-rwxrwx ---] score is:

  • owner = rwx = 4 + 2 + 1 = 7
  • group = rwx = 4 + 2 + 1 = 7
  • others = --- = 0 + 0 + 0 = 0

So wait a minute we set change permissions, authority figures in this document is 770 it! Change permissions chmod command syntax is this:

 chmod [-R] xyz 文件或目录

Options and parameters:

  • xyz: Permissions property is the numeric type just mentioned, as rwx property values ​​are added.
  • -R: Recursively (recursive) continues to change, that is, together with all documents will be sub-directory Change

For example, if you want the file .bashrc all permissions are set to enable, then 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

So, if you want permission to become -rwxr-xr-- it? Then scores authority becomes [4 + 2 + 1] [4 + 0 + 1] [4 + 0 + 0] = 754.

Symbol type change file permissions

There is also a way to change the permissions Yo! From the previous description, we can see that, basically nine privileges are (1) user (2) group (3) others Three Identities it! Then we can be u, g, o Three Identities of authority to represent!

In addition, a representative of all that is the whole identity! So read and write permissions can be written r, w, x! That is the way to look at the bottom can be used:

chmod u
g
o
a
+ (Added)
- (Remove)
= (Setting)
r
w
x
File or directory

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

[root@www ~]# ls -al .bashrc
-rwxr-xr-x  1 root root 395 Jul  4 11:45 .bashrc
[root@www ~]# chmod  a+w  .bashrc
[root@www ~]# ls -al .bashrc
-rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc

And if you want to remove permissions without changing the other permissions it already exists? For example, to remove all the people executable permissions, then:

[root@www ~]# chmod  a-x  .bashrc
[root@www ~]# ls -al .bashrc
-rw-rw-rw-  1 root root 395 Jul  4 11:45 .bashrc