1, users
-
Linux is a multi-user, multi task operating system
- Multiple users: multiple users log in at the same time
- Multitask: each user can perform multiple tasks
-
User:
- Management of Linux system
- Users are generally divided into user name and user ID (UID)
- The user name is convenient for the administrator to use, and the user ID is the only difference between a user (the computer only recognizes the ID)
-
File and process corresponding user relationship
- All users and processes in linux must have user correspondence.
- File existence must correspond to user
- Process running must correspond to user
-
Linux user classification
-
Super administrator: root
- UID is 0.
- If the user ID (UID) has root permission
[root@oldboyedu ~]# id root #View user ID d=0(root) gid=0(root) groups=0(root) [root@oldboyedu ~]# id uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@oldboyedu ~]# su - oldboy #Switching users [oldboy@oldboyedu ~]$ logout [root@oldboyedu ~]# vim /etc/passwd #Modify user ID root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sb in/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin oldboy:x:0:1000:oldboy:/home/oldboy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin "/etc/passwd" 22L, 1017C written [root@oldboyedu ~]# su - oldboy Last login: Fri Apr 10 18:52:44 CST 2020 from 10.0.0.1 on pts/0 [root@oldboyedu ~]# cd /root/ [root@oldboyedu /root]# cd .. [root@oldboyedu /]# ^C [root@oldboyedu /]# logout [root@oldboyedu ~]# id uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@oldboyedu ~]# su - oldboy Last login: Fri Apr 10 18:53:33 CST 2020 on pts/0 [root@oldboyedu ~]# id uid=0(root) gid=1000(oldboy) groups=1000(oldboy) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@oldboyedu ~]# whoami #Query current user root
- Note: do not change the UID value of users other than root to 0, which is easy to cause operation error due to too large permission
-
-
Virtual user
- The actual user, but it is not allowed to log in
- What is the value?
- Meet the requirements of file or process operation owner. But it will not bring management risk
- UID: 1-499
-
Ordinary users
-
The actual user allows him to log in, created as an administrator, and helps the administrator manage the system.
-
UID:1000~60000
[root@oldboyedu ~]# useradd oldgirl #Create a normal user [root@oldboyedu ~]# id oldgirl uid=1001(oldgirl) gid=1001(oldgirl) groups=1001(oldgirl)
-
1.1. How to find the lost root password?
- Restart the system. During the startup process, quickly press the direction keys and on the keyboard. The purpose is to inform the bootstrapper that we need to select different operations on the boot page in order to pause the bootstrapper.
- Press the e key on the keyboard to enter editing mode and find the line of Linux 16. Move the cursor all the way to the back of LANG=en_US.UTF-8, with a space, and then add init=/bin/sh. In particular, you need to write after UTF-8, keep the same line, and pay attention to the space. Because the screen is too small, it will automatically add \ line feed, which is normal.
- Mount root
- Select the user name to change the password, and select root to change it. You can change it to the user you want to change
-
Enter the same new password twice. Note that no characters will appear on the screen when entering the password.
- If the password you enter is too simple, you will be prompted to warn BAD PASSWORD: the password fails the dictionary check - it is too simple / systematic). You can ignore it and continue to enter the password, but it is recommended to set a more complex password to ensure security
-
Update system information touch /.authorelabel
-
Finally, enter the following command to restart the system
2. User groups
-
To assemble or organize (a person's family, for example)
-
Every user in Linux must have an organization, which is called user group
-
User groups are also shown to people. User groups also have unique identification. group ID is referred to as GID for short
-
By default, a user is created, but no user group is created. He will automatically create a user group, the same user group as the user name, and the same GID
[root@oldboyedu ~]# id oldgirl uid=1001(oldgirl) gid=1001(oldgirl) groups=1001(oldgirl)
-
2.1 correspondence between users and user groups
-
A user corresponds to a user group
-
One user corresponds to multiple user groups
-
Multiple users correspond to one user group
[root@oldboyedu ~]# useradd oldgirl [root@oldboyedu ~]# id oldgirl uid=1001(oldgirl) gid=1001(oldgirl) groups=1001(oldgirl) [root@oldboyedu ~]# useradd -g root text #Create user specified user group [root@oldboyedu ~]# id text uid=1002(text) gid=0(root) groups=0(root) [root@oldboyedu ~]# groupadd sa #Create user group [root@oldboyedu ~]# useradd -g sa uu [root@oldboyedu ~]# id uu uid=1003(uu) gid=1002(sa) groups=1002(sa) [root@oldboyedu ~]# id oldboy uid=1000(oldboy) gid=1000(oldboy) groups=1000(oldboy) [root@oldboyedu ~]# id root uid=0(root) gid=0(root) groups=0(root)
3. File permissions
-
Linux system has strict requirements on file security permission
[root@oldboyedu /]# ls -lih /etc/sysconfig/ total 108K 33575490 -rw-r--r--. 1 root root 412 Apr 4 18:09 anaconda 34063111 -rw-r--r--. 1 root root 483 Apr 4 18:08 authconfig 16841044 drwxr-xr-x. 2 root root 43 Apr 4 18:22 cbq 16881480 drwxr-xr-x. 2 root root 6 Aug 9 2019 console 33743063 -rw-r--r--. 1 root root 150 Mar 18 07:59 cpupower 34061028 -rw-------. 1 root root 110 Aug 9 2019 crond 34061967 -rw-------. 1 root root 1.4K Apr 11 2018 ebtables-config 1 2 3 4 5 6 7 8 9 10
-
Rwxrxrxrx: file permissions
- 9 characters, every three characters in a group
- The first three characters represent the permissions of the primary user
- The middle three characters represent the permissions of the user group
- The next three characters represent the permissions of other users
- r
- Read permission
- Represented by the number 4
- w
- Writable permissions
- Represented by the number 2
- x
- Executable rights
- Represented by the number 1
-
- No authority
- 9 characters, every three characters in a group
-
Column 789: last modified time of document
-
There are three file times
- Modification time: the content of modify file has been modified
- Access time: the time when the content of the access file has been accessed
- Change time: the time when the change file property changes
-
stat view files all the time
[root@oldboyedu ~]# stat /etc/hosts File: '/etc/hosts' Size: 158 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 16777794 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:net_conf_t:s0 Access: 2020-04-10 19:27:37.963000075 +0800 Modify: 2013-06-07 22:31:32.000000000 +0800 Change: 2020-04-04 18:05:04.725808731 +0800 Birth: -
-
-c access part
-
%A display character permission
[root@oldboyedu ~]# stat -c %A /etc/hosts -rw-r--r--
-
-
-
#Exercise 1. Modify access time and change time [root@oldboyedu ~]# echo oldboy > test.txt [root@oldboyedu ~]# stat test.txt File: 'test.txt' Size: 7 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 34060899 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2020-04-10 20:26:41.347028313 +0800 Modify: 2020-04-10 20:26:41.347028313 +0800 Change: 2020-04-10 20:26:41.347028313 +0800 Birth: - [root@oldboyedu ~]# echo oldboy >> test.txt [root@oldboyedu ~]# stat test.txt File: 'test.txt' Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 34060899 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2020-04-10 20:26:41.347028313 +0800 Modify: 2020-04-10 20:26:59.226028455 +0800 Change: 2020-04-10 20:26:59.226028455 +0800 Birth: - #Exercise 2. Change time [root@oldboyedu ~]# stat test.txt File: 'test.txt' Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 34060899 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2020-04-10 20:26:41.347028313 +0800 Modify: 2020-04-10 20:26:59.226028455 +0800 Change: 2020-04-10 20:26:59.226028455 +0800 Birth: - [root@oldboyedu ~]# chmod +x test.txt [root@oldboyedu ~]# stat test.txt File: 'test.txt' Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 34060899 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2020-04-10 20:26:41.347028313 +0800 Modify: 2020-04-10 20:26:59.226028455 +0800 Change: 2020-04-10 20:29:18.668029566 +0800
-
First column
-
File node
- The representation is the number 33574978, which is called the file index node
- Similar to ID card, index node is the unique identification of file in the system
- To find a file, you have to go through the index node to find it
- The concept of index node is derived from ext file system (ext2, ext3, ext4)
-
Index node
- The inode is a piece of storage space of the hard disk, with the size of 256 bytes or 512 bytes
- The data stored in the index node is the attribute of the file
- In the index node, only the file name is not included (the file name is in the block of the parent directory)
-
inode and block
- When the file system is created by formatting, a large number of inode s and block s will be generated
- inode stores the attribute information of the file and the pointer to the file entity (similar to soft connection)
- block is used to store the contents of files (large blocks)
- Block is called disk block, which is the entity unit used to store the actual data (the maximum size of ext file system is generally 4KB). That is to say, it is used to store the real data, such as ordinary file data such as photos and videos. A single large file needs to occupy multiple block blocks for storage. If a single file is too small to occupy the whole block, the remaining space cannot be used.
-
View inode
[root@oldboyedu /]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on devtmpfs 480007 370 479637 1% /dev tmpfs 482665 1 482664 1% /dev/shm tmpfs 482665 1227 481438 1% /run tmpfs 482665 16 482649 1% /sys/fs/cgroup /dev/sda3 9960960 72530 9888430 1% / /dev/sda1 131072 334 130738 1% /boot tmpfs 482665 1 482664 1% /run/user/0
-
View block
[root@oldboyedu /]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 12M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda3 19G 2.0G 18G 11% / /dev/sda1 253M 161M 93M 64% /boot tmpfs 378M 0 378M 0% /run/user/0
-
Two cases of disk full
- inode full (file inodes are full)
- block full
-
Tips:
- no space left no device reports an error. There is no space on the device (the disk is full.)
- Inspection steps
- df -h
- Df-i see if the inode is full
#Simulated disk full [root@oldboyedu ~]# mkdir -p /app/logs [root@oldboyedu ~]# dd if=/dev/zero of=/dev/sdc bs=8K count=10 10+0 records in 10+0 records out 81920 bytes (82 kB) copied, 0.000135709 s, 604 MB/s [root@oldboyedu ~]# mkfs.ext4 /dev/sdc mke2fs 1.42.9 (28-Dec-2013) /dev/sdc is not a block special device. Proceed anyway? (y,n) y Filesystem too small for a journal Discarding device blocks: done Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 16 inodes, 80 blocks 4 blocks (5.00%) reserved for the super user First data block=1 1 block group 8192 blocks per group, 8192 fragments per group 16 inodes per group Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done [root@oldboyedu ~]# mount -o loop /dev/sdc /app/logs/ [root@oldboyedu ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 52K 1.9G 1% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 12M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda3 19G 2.0G 18G 11% / /dev/sda1 253M 161M 93M 64% /boot tmpfs 378M 0 378M 0% /run/user/0 /dev/loop0 73K 14K 54K 21% /app/logs [root@oldboyedu ~]# cd /app/logs/ [root@oldboyedu /app/logs]# touch {1..15}.txt touch: cannot touch '6.txt': No space left on device touch: cannot touch '7.txt': No space left on device touch: cannot touch '8.txt': No space left on device touch: cannot touch '9.txt': No space left on device touch: cannot touch '10.txt': No space left on device touch: cannot touch '11.txt': No space left on device touch: cannot touch '12.txt': No space left on device touch: cannot touch '13.txt': No space left on device touch: cannot touch '14.txt': No space left on device touch: cannot touch '15.txt': No space left on device [root@oldboyedu /app/logs]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 52K 1.9G 1% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 12M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda3 19G 2.0G 18G 11% / /dev/sda1 253M 161M 93M 64% /boot tmpfs 378M 0 378M 0% /run/user/0 /dev/loop0 73K 15K 53K 23% /app/logs [root@oldboyedu /app/logs]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on devtmpfs 480007 375 479632 1% /dev tmpfs 482665 1 482664 1% /dev/shm tmpfs 482665 1233 481432 1% /run tmpfs 482665 16 482649 1% /sys/fs/cgroup /dev/sda3 9960960 72669 9888291 1% / /dev/sda1 131072 334 130738 1% /boot tmpfs 482665 1 482664 1% /run/user/0 /dev/loop0 16 16 0 100% /app/logs
- Why is inode full?
- Too many small files
- During initialization, the number of block s is far greater than the number of inode s
-
Emulated disk full
[root@oldboyedu /app/logs]# rm -rf * [root@oldboyedu /app/logs]# ll [root@oldboyedu /app/logs]# cp /bin/cat ddd [root@oldboyedu /app/logs]# cp /bin/cat ddddd cp: error writing 'ddddd': No space left on device cp: failed to extend 'ddddd': No space left on device [root@oldboyedu /app/logs]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 80K 1.9G 1% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 12M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda3 19G 2.0G 18G 11% / /dev/sda1 253M 161M 93M 64% /boot tmpfs 378M 0 378M 0% /run/user/0 /dev/loop0 73K 72K 0 100% /app/logs
-
-
The third column
-
Number of file hard connections
-
Number of normal file hard connections is 1
-
Number of directory hard connections is 2
-
Because there is also its own directory (.)
[root@oldboyedu /]# ls -lihd /data/ /data/. 34060900 drwxr-xr-x. 2 root root 137 Apr 10 20:36 /data/ 34060900 drwxr-xr-x. 2 root root 137 Apr 10 20:36 /data/.
-
-
-