Magedu - M46 - offline exercises

Posted by Jona on Tue, 11 Jan 2022 01:55:44 +0100

Offline exercises Chapter 11

Basic class
1. Edit a 1 Txt file, as follows

cat >>1.txt <<EOF
10.0.3.1 00:0F:AF:81:19:1F
10.0.3.2 00:0F:AF:85:6C:25
10.0.3.3 00:0F:AF:85:70:42
10.0.2.20 00:0F:AF:85:55:DE
10.0.2.21 00:0F:AF:85:6C:09
10.0.2.22 00:0F:AF:85:5C:41
10.0.0.151 00:0F:AF:85:6C:F6
10.0.0.152 00:0F:AF:83:1F:65
10.0.0.153 00:0F:AF:85:70:03
10.0.1.10 00:30:15:A2:3B:B6
10.0.1.11 00:30:15:A3:23:B7
10.0.1.12 00:30:15:A2:3A:A1
10.0.1.1 00:0F:AF:81:19:1F
10.0.2.2 00:0F:AF:85:6C:25
10.0.3.3 00:0F:AF:85:70:42
10.0.2.20 00:0F:AF:85:55:DE
10.0.1.21 00:0F:AF:85:6C:09
10.0.2.22 00:0F:AF:85:5C:41
10.0.0.151 00:0F:AF:85:6C:F6
10.0.1.152 00:0F:AF:83:1F:65
10.0.0.153 00:0F:AF:85:70:03
10.0.3.10 00:30:15:A2:3B:B6
10.0.1.11 00:30:15:A3:23:B7
10.0.3.12 00:30:15:A2:3A:A1
EOF

(1) Sort the output of the file (prompt: sort by the first character in the third column and all characters in the fourth column)

[root@Magedu ~]# 
[root@Magedu ~]# awk -F '[. ]' '{print $3,$4}' 1.txt  | sort -n
0 151
0 151
0 152
...
[root@Magedu ~]# #-k specifies the fields to be sorted
[root@Magedu ~]# #sort -t "." -k4.1,4.3 with '.' To separate and sort from the head of the fourth column to the end of the fourth column.
[root@Magedu ~]# sort -t "." -k3.1,3.1 -n -k4.1,4.3 -n 1.txt
10.0.0.151 00:0F:AF:85:6C:F6
10.0.0.151 00:0F:AF:85:6C:F6
10.0.0.152 00:0F:AF:83:1F:65
...
[root@Magedu ~]# 

(2) Filter all letters of the file, case insensitive

[root@Magedu ~]# 
[root@Magedu ~]# grep -i '[a-z]' 1.txt
10.0.3.1 00:0F:AF:81:19:1F
10.0.3.2 00:0F:AF:85:6C:25
10.0.3.3 00:0F:AF:85:70:42
...
[root@Magedu ~]# 

(3) Filter out lines ending with the number 3

[root@Magedu ~]# 
[root@Magedu ~]# grep '3$' 1.txt 
10.0.0.153 00:0F:AF:85:70:03
10.0.0.153 00:0F:AF:85:70:03
[root@Magedu ~]# 

2. Upload "web3_access.log" to your linux server
(1) Count the number of IP addresses of the file and sort them in positive order

[root@Magedu ~]# 
[root@Magedu ~]# awk '{print $1}' web3_access.log  | sort | uniq -c | sort -n 
      1 72.0.16.28
      1 172.18.118.99
      1 172.20.111.149
      2 172.16.101.126
		...
[root@Magedu ~]# 

(2) Count the number of HTTP status return codes in the file (for example, 200404403, in the ninth column) and sort them in reverse order

[root@Magedu ~]# 
[root@Magedu ~]# awk '{print $9}' web3_access.log | sort | uniq -c | sort -nr 
      1 91748
      3 501
      ...
  22893 206
  23395 200
[root@Magedu ~]# 

(3) Filter out all lines whose status return code is 200, and replace all lines whose return code is 200 with 300

[root@Magedu ~]# 
[root@Magedu ~]# awk '{print $9}' web3_access.log | grep '200' | sed 's#200#300#g'
300
300
300
...
[root@Magedu ~]# #grep -o displays only the part of the matched row that matches PATTERN.
[root@Magedu ~]# grep -o '200' web3_access.log |sed 's#200#300#g'
[root@Magedu ~]# 

3. Match the line containing the root keyword in / etc/passwd (at least two methods are required, awk and grep respectively)

[root@Magedu ~]# 
[root@Magedu ~]# awk -p '/root/'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Magedu ~]#
[root@Magedu ~]# 
[root@Magedu ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Magedu ~]# 

4. Take the ":" as the separator and take out the contents of the last column of the first row of / etc/passwd

[root@Magedu ~]# 
[root@Magedu ~]# awk -F ':' '{print $NF}' /etc/passwd | awk 'NR==1' 
/bin/bash
[root@Magedu ~]# awk -F ":" 'NR==1 {print $NF}' /etc/passwd
[root@Magedu ~]# 

5. Take out the column with ":" as the separator and the third column (user UID) ending with 0

[root@Magedu ~]# 
[root@Magedu ~]# awk -F ':' '{print $3}' /etc/passwd  | grep '0$'
0
990
70
1000
[root@Magedu ~]# 

6. Create new users u1 and u2. The password is redhat and belongs to the same additional group grp1. User u2 is not allowed to log in to the system

[root@Magedu ~]# 
[root@Magedu ~]# groupadd grp1
[root@Magedu ~]# useradd -G grp1 u1
[root@Magedu ~]# useradd -G grp1 u2
[root@Magedu ~]# usermod -s /sbin/nologin u2
[root@Magedu ~]# echo redhat | passwd --stdin u1
 Change user u1 Your password.
passwd: All authentication tokens have been successfully updated.
[root@Magedu ~]# echo redhat | passwd --stdin u2
 Change user u2 Your password.
passwd: All authentication tokens have been successfully updated.
[root@Magedu ~]# 

7. View uid and gid information of user u1

[root@Magedu ~]# 
[root@Magedu ~]# id u1
uid=1007(u1) gid=1008(u1) group=1008(u1),1007(grp1)
[root@Magedu ~]# 

8. Create group distro with GID 2019

[root@Magedu ~]# 
[root@Magedu ~]# groupadd -g 2019 distro
[root@Magedu ~]# 
[root@Magedu ~]# tail -n1 /etc/group
distro:x:2019:
[root@Magedu ~]# 

9. Create user magedudir with ID number of 1005 and basic group of distro

[root@Magedu ~]# 
[root@Magedu ~]# useradd -u 1005 -g 2019 magedudir
[root@Magedu ~]# 
[root@Magedu ~]# id magedudir
uid=1005(magedudir) gid=2019(distro) group=2019(distro)
[root@Magedu ~]# 

10. Add a password to user mageduman. The password is mageduboyedu

[root@Magedu ~]# 
[root@Magedu ~]# echo mageduboyedu | passwd --stdin magedudir 
Change user magedudir Your password.
passwd: All authentication tokens have been successfully updated.
[root@Magedu ~]# 

Process class
1. Use ps to view the process and explain the meaning of R, S, D, Z and T in the process status

[root@Magedu ~]# ps aux
R	Process running		s		  A process is a control process, Ss Process leader, parent process
S	Interruptible sleep		<		 The process runs on a high priority, S<Higher priority processes
T	The process is paused		N		 The process runs on a low priority, SN Lower priority processes
D	Non interruptible process		+		The current process is running in the foreground, R+Indicates that the process is running in the foreground
Z	Zombie process		l		 The process is multithreaded, Sl Indicates that the process is running as a thread

2. Install an nginx using yum and start it to view the details of the nginx process

[root@Magedu /]# 
[root@Magedu /]# yum install nginx
Repository extras is listed more than once in the configuration
 Last metadata expiration check: 0:00:48 It was executed at 19:49:17 on Tuesday, July 27, 2021.
Dependency resolution.
...
complete!
[root@Magedu /]# 
[root@Magedu /]# systemctl start nginx
[root@Magedu /]# 
[root@Magedu /]# ps aux | grep nginx
root       2322  0.0  0.0  46472  1140 ?        Ss   17:48   0:00 nginx: master process /usr/sbin/ngin -c /etc/nginx/nginx.conf
nginx      2323  0.0  0.1  48952  2248 ?        S    17:48   0:00 nginx: worker process
root       4650  0.0  0.0 112728   972 pts/0    S+   19:30   0:00 grep --color=auto nginx
[root@Magedu /]# 

3. Open two xshell terminals to connect to the same virtual machine. One terminal enters the command "yum update", the other terminal checks the status of the yum command, the other terminal enters "yum install -y mariadb", and then checks the status of the yum command. When the yum command is completed, check the status of the yum command again.

[root@Magedu /]# 
[root@Magedu /]# yum update -y
 Plug in loaded: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
...
[root@Magedu /]#
[root@Magedu /]# 
[root@Magedu /]# ps aux |grep yum
root       2549  105  8.5 497324 159988 pts/1   R+   05:46   0:07 /usr/bin/python /usr/bin/yum update -y
root       2552  0.0  0.0 112728   972 pts/0    R+   05:46   0:00 grep --color=auto yum
[root@Magedu /]#
[root@Magedu /]# 
[root@Magedu /]# ps aux |grep yum
root       2557  0.0  0.0 112728   972 pts/0    S+   05:46   0:00 grep --color=auto yum
[root@Magedu /]# 

------------------------------------------------------------

[root@Magedu /]# 
[root@Magedu /]# yum install -y mariadb
 Plug in loaded: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
...
[root@Magedu /]# 
[root@Magedu /]# 
[root@Magedu /]# ps aux |grep yum
root       2655 98.0  5.6 434552 104944 pts/0   R+   05:52   0:01 /usr/bin/python /usr/bin/yum install -y mariadb
root       2658  0.0  0.0 112728   972 pts/1    S+   05:52   0:00 grep --color=auto yum
[root@Magedu /]# 
[root@Magedu /]# 
[root@Magedu /]# ps aux |grep yum
root       2660  0.0  0.0 112728   972 pts/0    S+   05:52   0:00 grep --color=auto yum
[root@Magedu /]# 

4. Use "vim test.c" to edit the code as follows

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
pid_t pid;
pid = fork();
if (pid == 0) {
int iPid = (int)getpid();
fprintf(stderr,"I am child,%d\n",iPid);
sleep(1);
fprintf(stderr, "Child exits\n");
return EXIT_SUCCESS;
}
int iPid = (int)getpid();
fprintf(stderr,"I am parent,%d\n",iPid);
fprintf(stderr, "sleep....\n");
sleep(600);
fprintf(stderr, "parent exits\n");
return EXIT_SUCCESS;
}

Then use "gcc test.c" to compile this code. A.out file will be generated in the current directory. Use ". / a.out" to execute the file (if there is no gcc command, use yum to install)
(1) Open a new terminal and use the top command to check the process status and whether there is a zombie process

[root@Magedu ~]# 
[root@Magedu ~]# gcc test.c 
[root@Magedu ~]# 
[root@Magedu ~]# ./a.out
I am parent,2813
sleep....
I am child,2814
Child exits
[root@Magedu ~]# 
[root@Magedu ~]# top
  ...
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND             
  2898 root      20   0  162028   2332   1588 R   0.3  0.1   0:00.06 top                 
  2868 root      20   0       0      0      0 Z   0.0  0.0   0:00.00 a.out               
  2867 root      20   0    4212    348    276 S   0.0  0.0   0:00.00 a.out
  2838 root      20   0  123264    736    508 S   0.0  0.0   0:00.00 anacron
  ...
[root@Magedu ~]# 

(2) Use the ps command to view the status of the zombie process

[root@Magedu ~]# 
[root@Magedu ~]# ps aux | grep out
root       2867  0.0  0.0   4212   348 pts/1    S+   06:02   0:00 ./a.out
root       2868  0.0  0.0      0     0 pts/1    Z+   06:02   0:00 [a.out] <defunct>
root       2884  0.0  0.0 112728   972 pts/0    S+   06:03   0:00 grep --color=auto out
[root@Magedu ~]# 

(3) Use the kill command to try to terminate the zombie process (do not use the - 9 parameter to force termination), and then check the status of the process to see if it can be terminated

[root@Magedu ~]# 
[root@Magedu ~]# ps -ef |grep  out
root       4869   4788  0 19:37 pts/0    00:00:00 ./a.out
root       4870   4869  0 19:37 pts/0    00:00:00 [a.out] <defunct>
root       4872   4705  0 19:37 pts/2    00:00:00 grep --color=auto out
[root@Magedu ~]# 
[root@Magedu ~]# kill 4870
[root@Magedu ~]# 
[root@Magedu ~]# ps -ef |grep  out
root       4869   4788  0 19:37 pts/0    00:00:00 ./a.out
root       4870   4869  0 19:37 pts/0    00:00:00 [a.out] <defunct>
root       4874   4705  0 19:37 pts/2    00:00:00 grep --color=auto out
[root@Magedu ~]# 

(4) Kill the parent process of the zombie process and check whether the process is terminated

[root@Magedu ~]# 
[root@Magedu ~]# ps -ef |grep  out
root       4852   4788  0 19:36 pts/0    00:00:00 ./a.out
root       4853   4852  0 19:36 pts/0    00:00:00 [a.out] <defunct>
root       4855   4705  0 19:37 pts/2    00:00:00 grep --color=auto out
[root@Magedu ~]# 
[root@Magedu ~]# kill 4852
[root@Magedu ~]# 
[root@Magedu ~]# ps -ef |grep  out
root       4866   4705  0 19:37 pts/2    00:00:00 grep --color=auto out
[root@Magedu ~]# 

5. Enter the top command to explain the meaning represented by each line and column of the first six lines (not an empty line)

[root@Magedu ~]# 
[root@Magedu ~]# top
top - 06:04:37 up 44 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 169 total,   1 running, 167 sleeping,   0 stopped,   1 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863104 total,   392584 free,   418152 used,  1052368 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1275348 avail Mem 

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND  
  1 root      20   0  191240   4320   2640 S   0.0  0.2   0:03.76 systemd
...
[root@Magedu ~]# 
#The first line top - 06:04:37 up 44 min, 2 users, load average: 0.00, 0.01, 0.05
 Task queue information, the same as uptime The execution result of the command.
06:04:37 Current time 
up 44 min System running time in the format of:Points 
2 users Number of currently logged in users 
load average: 0.00, 0.01, 0.05 System load, that is, the average length of the task queue. The three values are the average values from 1 minute, 5 minutes and 15 minutes ago to now.
#Tasks in the second line: 169 total, 1 running, 167 sleeping, 0 stopped, 1 zombie
169 total Total number of processes 
1 running Number of running processes 
167 sleeping Number of sleep processes 
0 stopped Number of processes stopped 
1 zombie Number of zombie processes.
#The third line% CPU (s): 0.0 us, 0.2 sy, 0.0 Ni, 99.8 ID, 0.0 WA, 0.0 Hi, 0.0 Si, 0.0 st
0.0 us User space occupation CPU Percentage 
0.2 sy Kernel space occupation CPU Percentage 
0.0 ni Occupied by processes with changed priority in user process space CPU Percentage 
99.8 id free CPU Percentage 
0.0 wa Waiting for input and output CPU Percentage of time 
0.0 hi Hard interrupt( Hardware IRQ)occupy CPU Percentage of 
0.0 si Soft interrupt( Software Interrupts)occupy CPU Percentage of 
0.0 st
#Line 4 KIB MEM: 1863104 total, 392584 free, 418152 used, 1052368 buff / cache
KiB Mem:1863104 total Total physical memory 
392584 free Total free memory 
418152 used Total physical memory used 
1052368 buff/cache The amount of memory used as the kernel cache.
#Line 5 KIB swap: 2097148 total, 2097148 free, 0 used 1275348 avail Mem 
KiB Swap:2097148 total Total exchange area 
2097148 free Total free swap area 
0 used. Total number of exchange areas used 
1275348 avail Mem Represents the amount of physical memory available for the next allocation of the process 
cached Mem Total number of swap buffers.
#Line 6 PID user PR Ni virt res SHR s% CPU% MEM time + command  
PID process ID, 
USER User name of the process owner 
PR Priority 
NI nice Value. Negative values indicate high priority, positive values indicate low priority 
VIRT The total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES, 
RES The size of physical memory used by the process and not swapped out, unit kb. RES=CODE+DATA, 
SHR Shared memory size in kb, S Process status. D=Non interruptible sleep state R=function S=sleep T=track/stop it Z=Zombie process %CPU Last updated to now CPU Percentage of time occupied 
%MEM Percentage of physical memory used by the process 
TIME+ Used by the process CPU Total time in 1/100 Seconds 
COMMAND Command name/Command line. 
#------------Supplement------------
Listing		meaning
PID		process id
PPID	Parent process id
RUSER	Real user name
UID		User of the process owner id
USER	User name of the process owner
GROUP	Group name of the process owner
TTY		The name of the terminal that started the process. Processes that are not started from the terminal are displayed as ?
PR		priority
NI		nice Value. Negative values indicate high priority and positive values indicate low priority
P		Last used CPU,Only in many CPU Meaningful in the environment
%CPU	Last updated to now CPU Time occupancy percentage
TIME	Used by the process CPU Total time in seconds
TIME+	Used by the process CPU Total time in 1/100 second
%MEM	Percentage of physical memory used by the process
VIRT	The total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES
SWAP	The size, unit, of the virtual memory used by the process kb
RES		The size of physical memory used by the process and not swapped out, unit kb. RES=CODE+DATA
CODE	Physical memory occupied by executable code, unit kb
DATA	Parts other than executable code(Data segment+Stack)Amount of physical memory occupied, in kb
SHR		Shared memory size in kb
nFLT	Number of page errors
nDRT	The number of pages that have been modified since the last write.
S		Process status. D=Non interruptible sleep state R=function S=sleep T=track/stop it Z=Zombie process
COMMAND	Command name/command line
WCHAN	If the process is sleeping, the system function name in sleep is displayed
Flags	Task flag

6. Open two terminals, enter the top command in one terminal, and the other terminal terminates the process to see the effect

[root@Magedu ~]# 
[root@Magedu ~]# top
	top - 21:39:26 up 16:34,  3 users,  load average: 0.03, 0.02, 0.05
	...
     PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND        
     1 root      20   0  125880   4180   2472 S  0.3  0.2   0:13.00 systemd               
	...
	Killed
[root@Magedu ~]# 
----------------------------------------------------------------------
[root@Magedu ~]# ps -ef | grep top
root      71164  59828  0 21:39 pts/5    00:00:00 top
root      71203  71112  0 21:39 pts/1    00:00:00 grep --color=auto top
[root@Magedu ~]# 
[root@Magedu ~]# kill -9 71164
[root@Magedu ~]# 

7. Enter the following command in linux "(while:; do uptime; sleep 1; done) &", open a new terminal to view the status of the process and try to terminate the process

[root@Magedu ~]# 
[root@Magedu ~]# ps aux | grep sleep
root       3319  0.0  0.0 107956   612 ?        S    06:17   0:00 sleep 60
root       3406  0.0  0.0 107956   612 pts/0    S    06:18   0:00 sleep 1
root       3408  0.0  0.0 112728   972 pts/1    S+   06:18   0:00 grep --color=auto sleep
[root@Magedu ~]# 
[root@Magedu ~]# pkill -9 -t pts/0
[root@Magedu ~]# 
[root@Magedu ~]# ps aux | grep sleep
root       3319  0.0  0.0 107956   612 ?        S    06:17   0:00 sleep 60
root       3434  0.0  0.0 112728   968 pts/1    S+   06:18   0:00 grep --color=auto sleep
[root@Magedu ~]# 

Topics: Linux Operation & Maintenance CentOS sre