Linux study notes Part2 Linux file, directory and disk format (1)

1. File permissions and directory configuration

1. User type

(1) File owner

(2) User group

(3) Others

(4) root user with the highest authority

​ Switch to root user: su command

User information is recorded in /etc/passwd Personal password is recorded in /etc/shadow Group name is recorded in /etc/group

2. File permissions

(1) File attributes

Example: Information obtained through the ls -al command

​ -rw-r–r--. 1 root root 1864 May 4 18:01 initial-setup-ks.cfg

​ File type permission number of links file owner file user group file size file last modified time file name

The first column represents the file permission type:

-Rwxrwx—

​ The file permission type consists of ten characters

The first character indicates that the file is a directory, file, or link file:

  • ​ d is a directory,-is a file, l is a link file, b is a peripheral device that can be stored in the device file, and c is a serial port device

In the following characters, three are a group, and they are all three parameter combinations of [rwx].

  • ​ r stands for readable, w stands for writable, x stands for executable
  • ​ The position of the permission will not change, if there is no permission, the symbol-
  • ​ The first group is the permissions that the file owner can have
  • ​ The second group is the permission to join this user group account
  • ​ The third group is non-self and does not have permission to join other accounts in this user group (others)
The second column represents how many file names are linked to the node
The third column represents the account of the owner of this file (or directory)
The fourth column indicates the user group to which this file belongs
The fifth column is the size of the file, the default unit is Bytes
The sixth column is the creation date or the most recent modification date of this file
The seventh column is the file name
  • ​ If there is one more before the file name. It means that the file is a hidden file
The importance of file permissions:
  • System protection function
  • Team development software or data sharing function
  • safety

(2) Modify file permissions and attributes

chgrp: modify the user group to which the file belongs
chgrp [-R] dirname/filename ...
参数:
	-R : 进行递归( recursive )的持续变更,亦即连同次目录下的所有档案、目录都更新成为这个群组之意。常常用在变更某一目录的情况。 
范例:
[root@linux ~]# chgrp users install.log
[root@linux ~]# ls -l
-rw-r--r-- 1 root users 68495 Jun 25 08:53 install.log
[root@linux ~]# chgrp testing install.log
chgrp: invalid group name `testing' <== 发生错误讯息啰~找不到这个群组名~ 
chown: modify the file owner
chown [-R] 账号名称 文件或目录
chown [-R] 账号名称:用户组名称 文件或目录
参数:
	-R : 进行递归( recursive )的持续变更,亦即连同次目录下的所有档案、目录都更新成为这个群组之意。常常用在变更某一目录的情况。
范例:
[root@linux ~]# chown bin install.log
[root@linux ~]# ls -l
-rw-r--r-- 1 bin users 68495 Jun 25 08:53 install.log
[root@linux ~]# chown root:root install.log
[root@linux ~]# ls -l
-rw-r--r-- 1 root root 68495 Jun 25 08:53 install.log
chmod: modify file permissions

There are 9 basic permissions for Linux files, namely the owner, user group, and others. Each of the three identities has its own read, write, and execute.

The number of each authority is as follows:

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

The three authority numbers for each identity are cumulative:

Example: When the attribute is [-rwxrwx—]

Owner = rwx = 4+2+1 = 7
User group belonging = rwx = 4+2+1 = 7
Others = — = 0+0+0 = 0

So when we set the permissions, the permission number of the file is 770

The syntax of the chmod command is:

chmod [-R] xyz 档案或目录
参数:
xyz : 就是刚刚提到的数字类型的权限属性,为 rwx 属性数值的相加。
-R : 进行递归( recursive )的持续变更,亦即连同次目录下的所有档案、目录
 都更新成为这个群组之意。常常用在变更某一目录的情况。 

例子:如果要将 .bashrc 这个档案所有的属性都打开,那么就下达:
[root@linux ~]# ls -al .bashrc
-rw-r--r-- 1 root root 395 Jul 4 11:45 .bashrc
[root@linux ~]# chmod 777 .bashrc
[root@linux ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc 

What if the attribute is changed to "-rwxr-xr-- "? Then it becomes [4+2+1] [4+0+1] [4+0+0]=754, so you need to issue chmod 754 filename.

Another way to modify:

  • The three identities (owner user, user group group, and other people) correspond to the letters u, g, o and all identities (all) for the letter a
  • Permission can be written as r, w, x
  • Add a permission to use the symbol +
  • Remove a permission symbol-
  • Set a permission to use the symbol =

Example: To set the permission of a certain file to [-rwxr-xrx]:

user: has readable, writable and executable permissions

group and other: have read and execute permissions

[root@linux ~]# chmod u=rwx,go=rx .bashrc
# 注意喔!那个 u=rwx,go=rx 是连在一起的,中间并没有任何空格符!
[root@linux ~]# ls -al .bashrc
-rwxr-xr-x 1 root root 395 Jul 4 11:45 .bashrc 

So if it is "-rwxr-xr-- "? It can be set using "chmod u=rwx,g=rx,o=r filename".

If you don’t know the original attributes of the file, you just want to add permissions that everyone can write:

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

If you want to remove the permissions without modifying other existing permissions, for example, you want to remove the executable permissions of everyone:

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

(3) The meaning of directory and file permissions

The meaning of file permissions:
  • r: The actual content of this file can be read
  • w: You can edit, add or modify the content of the file (but not delete the file)
  • x: The file has permissions that can be executed by the system
The meaning of directory permissions:
  • r: means having the permission to read the directory structure list, you can query the file name data in the directory, and you can display the content list of the directory through the ls command
  • w: Indicates the authority to change the list of the directory structure: create new files and directories, delete existing files and directories, rename existing files or directories, move files and directory locations in the directory
  • x: represents whether the user can enter the directory and become the working directory

(4) Linux file types and extensions

File type
  • Regular file: The first symbol is-. Including: plain text file (ASCLL), binary file (binary), data file (data)
  • Directory: The first symbol is d.
  • Link file: The first attribute is l, similar to the shortcut of windows system
  • Equipment and equipment files: usually concentrated in the /dev directory. Block device file: The first attribute is b, which provides an interface device for system random access. Character device file: the first attribute is c, the interface device of the serial port
  • Interface data file: The first attribute is s, which is usually used for data exchange on the Internet, usually in /tmp or /run
  • Data transfer file: The first attribute is p, which is mainly used to solve the error problem caused by multiple programs reading and writing a file at the same time
Linux file extension
  • The LInux file has no so-called extension. Whether a Linux program can be executed is related to the ten attributes in the first column of it, and has nothing to do with the file name. As long as you have x in the permissions, it means that the file has the ability to be executed.
Linux file name length limit
  • The maximum allowable file name of a single file or directory is 255 bytes, and if an ASCII English occupies one byte, it can be up to 255 characters in length. If each Chinese character occupies two bytes, the maximum file name is about 128 Chinese characters.
Linux file name restrictions
  • Avoid special characters
* ? > < ; & ! [ ] | \ ' " ` ( ) { }
  • At the same time, it is best to avoid starting with-or +

3. Linux directory configuration

(1) The basis of Linux directory configuration-FHS

Purpose: I hope that users can understand which directory the installed software is usually placed in
In fact, it only specifies what kind of directories should be placed in the main directories under the root directory (/ ). FHS defines two levels of specifications:
  • ​ The first level is / (root directory: related to the system startup system). What kind of file data should be placed in each directory under the / (root directory: related to the system startup).
  • ​ The second layer is defined for the subdirectories of the two directories /usr (unix software resource: related to software installation and execution) and /var (variable: related to the system running process).
The four types of directory interaction defined by FHS:
  • Shareable: can be shared to other systems to mount and use the directory
  • Unshareable: device files or socket files related to programs that operate on your own machine are only related to your own machine, so of course it is not suitable for sharing with other hosts
  • Variable: data that changes frequently, such as login files, newsgroups that general users can receive by themselves, etc.
  • Unchangeable: Some data will not change frequently, and follow the distribution without changing. For example, function library, file description file, host service configuration file managed by system administrator, etc.
The meaning and content of the root directory (/)

​ Related to the system startup system

​ Significance: The root directory is the most important directory in the entire system, because not only all directories are derived from the root directory, but the root directory is also related to boot/restore/system repair and other actions.

​ FHS recommendations: the smaller the partition where the root directory is located, the better, and the software installed by the application should not be placed in the same partition as the root directory. This will not only have better performance, but also the file system where the root directory is located is less prone to problems. .

FHS requires that the directory must exist:

  • /bin: Place the instructions that can be operated in single-user maintenance mode

  • /boot: Place the files that are used to start the opportunity

  • /dev: The device file placed, accessing a file in this directory is equivalent to accessing a device

  • /etc: Place the main configuration files of the system

  • /home: This is the default user home folder of the system. The home folder has two codes: ~ represents the home folder of the current user, ~boatman represents the home folder of the user boatman

  • /lib: There are many system functions, and /lib places the function libraries that will be used when the system is booted, and the function libraries that will be called by the commands under /bin or /sbin. Pay particular attention to the /lib/modules directory , The kernel-related modules (drivers) will be placed below it

  • /media: The file corresponding to the deleteable device is placed; including: CD, DVD and other devices are temporarily mounted here

  • /mnt: If you want to temporarily mount some additional devices, it is generally recommended that you put them in this directory; in the early days, the purpose of this directory is the same as /media! Only after having /media, this The directory is used for temporary mounting

  • /opt: Third-party software installation directory (However, in previous Linux systems, we are still used to place it in the /usr/local directory)

  • /root: The home folder of the system super administrator (root) (the reason why it is placed here is because if you enter the single-user maintenance mode and only mount the root directory, the root directory can have the root home folder, so we (I hope root's home folder and root directory are placed in the same partition)

  • /sbin: Place the commands needed during the boot process, which includes the commands needed for booting, repairing, and restoring the system.
    As for some server software programs, they are generally placed in /usr/sbin/ as
    for the local installation. The system executable file (system binary) generated by the software is placed in /usr/local/sbin/

  • /srv: srv can be regarded as the abbreviation of "service", which is the data directory that some network services need to access after they are started (for example, the webpage data required by the WWW service can be placed in /srv/www/)

  • /tmp: system temporary program, data directory

  • /usr: related to software installation and execution

  • /var: related to the running process of the system

Catalogs that are not defined in the FHS are also very important:

  • /lost+found: This directory is a directory generated only when the standard ext2/ext3/ext4 file system format is used. The purpose is to place some missing fragments in this directory when an error occurs in the file system, but if you use it If it is an xfs file system, this directory will not exist
  • /proc: This directory itself is a virtual file system. The data it places are all in memory, such as system kernel, process information, status of external devices, and network status. Because the data under this directory is in the memory, it does not occupy any hard disk space.
  • /sys: This directory is actually very similar to /proc. It is also a virtual file system, which mainly records information related to the kernel. Including currently loaded kernel modules and hardware settings information detected by the kernel, etc. This directory also does not occupy hard disk capacity
The meaning and content of /usr

​ Related to software installation and execution

​ The data placed in /usr is shareable and immutable (shareable, static). If you know how to mount the partition through the network (NFS server), then /usr can indeed be shared with other parts of the local area network Host to use

​ FHS Recommendation: Software developers should place their data in subdirectories of this directory separately, instead of creating their own independent directory for the software.

​ FHS requires that the directory must exist:

  • /usr/bin/ All commands that can be used by general users are placed here. At present, the new CentOS 7 has placed all user commands here, and the paradigm of link files is used to link /bin to this. In other words, /usr/bin and /bin are exactly the same. In addition, FHS requires that there should be no subdirectories under this directory.
  • /usr/lib/: basically the same function as /lib, so /lib is linked to this directory
  • /usr/local: The system administrator installs the software (provided by default in the non-release version) on this machine. It is recommended to install to this directory, which will be easier to manage.
  • /usr/sbin/: System commands not required for the normal operation of the system, the most common ones are the service commands (daemon) of some web server software. But the basic function is similar to /sbin, so currently /sbin is linked to this directory
  • /usr/share/: Mainly store read-only data files, including shared files. The data placed in this directory is almost readable data regardless of the hardware architecture. Common subdirectories under this directory: /usr/share/doc: software documentation; /usr/share/man: online help documents; /usr/share/zoneinfo: time zone files related to time zones
The meaning and content of /var

​ System operation process related

​ /var is a directory that gradually occupies the capacity of the hard disk after the system is running, because the /var directory is mainly for files that change frequently, including caches, log files, and some software operations. Files, including program files (lock file, run file), or files such as MySQL database, etc.

​ FHS requires that the directory must exist:

  • /var/cache/: Some caches generated during the operation of the application itself
  • /var/lib/: The directory where the data files that need to be used are placed during the execution of the program itself. Each software should have its own catalog under this catalog.
  • /var/lock/: Some equipment or file resources can only be used by one application at a time. If two programs use the device at the same time, some error conditions may occur, so you have to install the device Lock (lock) to ensure that the device will only be used by a single software.
  • /var/log/: Very important. This is the directory where the log files are placed. The more important files in it are /var.log.messages, /var/log/wtmp (login record information), etc.
  • /var/mail/: The directory where personal e-mail mailboxes are placed, but this directory is also placed in /var/spool/mail/ directory! Usually these two directories are mutually linked files
  • /var/run/: After some programs or services are started, their PIDs will be placed in this directory
  • /var/spool/: This directory usually contains some queue data. The so-called queue is the data queued for use by other programs. These data are usually deleted after being used.
    If it is a scheduled task data (crontab), it will be placed in the /var/spool/cron/ directory

(2) Directory tree

Under Linux, all files and directories start from the root directory /, which is the source of all directories and files, and then branch off one by one, a bit like a tree branch. Therefore, we also call this type of directory configuration: directory tree.
characteristic:
  • The starting point of the directory tree is the root directory (/, root);
  • Each directory can use not only the file system of the local partition, but also the file system on the network.
  • The file name (including the full path) of each file in this directory tree is unique.

Example: Data in the root directory of CentOS

[lqq@localhost ~]$ ls -l /
总用量 32
lrwxrwxrwx.   1 root root    7 2月   4 12:08 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 2月  19 11:01 boot
drwxr-xr-x.   4 root root   28 2月  17 19:14 data
drwxr-xr-x.  20 root root 3380 2月  20 11:40 dev
drwxr-xr-x. 141 root root 8192 2月  20 11:40 etc
drwxr-xr-x.   5 root root   41 2月  20 11:55 home
lrwxrwxrwx.   1 root root    7 2月   4 12:08 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 2月   4 12:08 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   3 root root   18 2月   6 20:31 mnt
drwxr-xr-x.   5 root root   46 2月   4 12:37 opt
dr-xr-xr-x. 244 root root    0 2月  20 11:39 proc
dr-xr-x---.  10 root root 4096 2月  20 11:46 root
drwxr-xr-x.  43 root root 1320 2月  20 11:45 run
lrwxrwxrwx.   1 root root    8 2月   4 12:08 sbin -> usr/sbin
drwxr-xr-x.   4 root root   71 2月  19 11:01 srv
dr-xr-xr-x.  13 root root    0 2月  20 11:39 sys
drwxrwxrwt.  52 root root 8192 2月  20 12:10 tmp
drwxr-xr-x.  13 root root  155 2月   4 12:08 usr
drwxr-xr-x.  20 root root  282 2月   4 12:18 var

(3) Relative path and absolute path

Absolute path: the file name or directory name written from the root directory (/), for example /home/dmtsai/.bashrc
Relative path: The file name is written relative to the current path. For example. /home/dmtsai or.../.../home/dmtsai/

​ does not start with /, it belongs to the writing of a relative path

Two special directories:
  • .: Represents the current directory, and can also be represented by ./.
  • …: represents the upper level directory, and can also be represented by …/.
Example: You are currently in the /home directory. If you want to enter the /var/log directory, how can you write it?
  • cd / var / log
  • cd… / var / log

2. Linux file and directory management

Guess you like

Origin blog.csdn.net/Qingyuyuehua/article/details/113883935