本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
第十一章 进程和计划任务
1.如何查看进程是多线程还是单线程
[root@rocky8 ~]# cat /proc/723/status |grep -i threads
Threads: 6 # Threads 线程
2.僵尸态
[root@rocky8 ~]# bash
[root@rocky8 ~]# echo $BASHPID
9454
[root@rocky8 ~]# echo $PPID
1312
[root@rocky8 ~]# pstree -p |grep bash
|-sshd(765)---sshd(1299)---sshd(1311)---bash(1312)---bash(9454)-+-grep(9472)
#新开一个终端
[root@rocky8 ~]# pstree -p |grep bash
|-sshd(765)-+-sshd(1299)---sshd(1311)---bash(1312)---bash(9454)
| `-sshd(9473)---sshd(9475)---bash(9476)-+-grep(9498)
[root@rocky8 ~]# ps aux |grep bash
root 1312 0.0 0.4 26244 4000 pts/0 Ss 13:04 0:00 -bash
root 9454 0.0 0.4 26216 3924 pts/0 S+ 13:45 0:00 bash
root 9476 0.0 0.4 26244 3812 pts/1 Ss 13:47 0:00 -bash
root 9500 0.0 0.1 12136 1156 pts/1 R+ 13:47 0:00 grep --color=auto bash
#将父进程设为停止态
[root@rocky8 ~]# kill -19 1312
[root@rocky8 ~]# ps aux |grep bash
root 1312 0.0 0.4 26244 4000 pts/0 Ts 13:04 0:00 -bash
root 9454 0.0 0.4 26216 3924 pts/0 S+ 13:45 0:00 bash
root 9476 0.0 0.4 26244 3812 pts/1 Ss 13:47 0:00 -bash
root 9502 0.0 0.1 12136 1152 pts/1 R+ 13:48 0:00 grep --color=auto bash
#杀死子进程,使其进入僵尸态
[root@rocky8 ~]# kill 9454
[root@rocky8 ~]# ps aux |grep bash
root 1312 0.0 0.4 26244 4000 pts/0 Ts 13:04 0:00 -bash
root 9454 0.0 0.0 0 0 pts/0 Z+ 13:45 0:00 [bash] <defunct> #可以看到上STAT为Z,表示为僵尸态
root 9476 0.0 0.4 26244 3812 pts/1 Ss 13:47 0:00 -bash
root 9505 0.0 0.1 12136 1156 pts/1 R+ 13:49 0:00 grep --color=auto bash
[root@rocky8 ~]# kill 1312
[root@rocky8 ~]# ps aux |grep bash
root 1312 0.0 0.4 26244 4000 pts/0 Ts 13:04 0:00 -bash
root 9454 0.0 0.0 0 0 pts/0 Z+ 13:45 0:00 [bash] <defunct>
root 9476 0.0 0.4 26244 3812 pts/1 Ss 13:47 0:00 -bash
root 9507 0.0 0.1 12136 1036 pts/1 R+ 13:50 0:00 grep --color=auto bash
#方法1:恢复父进程
[root@rocky8 ~]# kill -18 1312
#方法2:杀死父进程
[root@rocky8 ~]# kill -9 1312
#再次观察,可以僵尸态的进程不存在了
[root@rocky8 ~]# ps aux |grep bash
root 9476 0.0 0.4 26244 3812 pts/1 Ss 13:47 0:00 -bash
root 9512 0.0 0.4 26244 4000 pts/0 Ss+ 13:50 0:00 -bash
root 9534 0.0 0.1 12136 1140 pts/1 R+ 13:50 0:00 grep --color=auto bash
3.面试题:找到未知进程的执行程序文件路径
[root@rocky8 ~]# echo $BASHPID
10452
[root@rocky8 ~]# ls -l /proc/10452/exe
lrwxrwxrwx 1 root root 0 Oct 31 14:59 /proc/10452/exe -> /usr/bin/bash
4.搜索某个用户运行的进程
root@ubuntu2004:~# pgrep -au neteagle
5376 -bash
5.查看某个端口正在被哪个进程使用
root@ubuntu2004:~# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 900 root 3u IPv4 34154 0t0 TCP *:ssh (LISTEN)
sshd 900 root 4u IPv6 34156 0t0 TCP *:ssh (LISTEN)
sshd 1108 root 4u IPv4 36999 0t0 TCP ubuntu2004:ssh->10.0.0.1:57525 (ESTABLISHED)
sshd 3667 root 4u IPv4 52331 0t0 TCP ubuntu2004:ssh->10.0.0.1:58047 (ESTABLISHED)
6.面试题:11月每天的6-12点之间每隔2小时执行/app/bin/test.sh
root@ubuntu2004:/# crontab -l
0 6-12/2 * 11 * /app/bin/test.sh
第十二章 Linux启动和内核管理
1.centos6启动流程
1.加载BIOS的硬件信息,获取第一个启动设备
2.读取第一个驱动设备MBR的引导加载程序(grub)的启动信息
3.加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有硬件设备
4.核心执行init程序,并获取默认的运行信息
5.init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统
6.启动核心的外挂模块
7.init执行运行的各个批处理文件(scripts)
8.init执行/etc/rc.d/rc.local
9.执行/bin/login程序,等待用户登录
10.登录之后开始以shell控制主机
2.centos7、8启动流程
1.UEFi或BIOS初始化,运行POST开机自检
2.选择启动设备
3.引导装载程序, centos7是grub2,加载装载程序的配置文件: /etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
4.加载initramfs驱动模块
5.加载内核选项
6.内核初始化,centos7使用systemd代替init
7.执行initrd.target所有单元,包括挂载/etc/fstab
8.从initramfs根文件系统切换到磁盘根目录
9.systemd执行默认target配置,配置文件/etc/systemd/system/default.target
10.systemd执行sysinit.target初始化系统及basic.target准备操作系统
11.systemd启动multi-user.target下的本机与服务器服务
12.systemd执行multi-user.target下的/etc/rc.d/rc.local
13.Systemd执行multi-user.target下的getty.target及登录服务
14.systemd执行graphical需要的服务
第十三章 Linux防火墙
1.连接过多不能访问
当服务器连接多于最大连接数时dmesg 可以观察到 :kernel: ip_conntrack: table full, dropping packet错误,并且导致建立TCP连接很慢。
[root@rocky8 ~]# echo 1 > /proc/sys/net/netfilter/nf_conntrack_max
[root@rocky8 ~]# tail /var/log/messages
Jul 9 21:06:32 centos8 kernel: nf_conntrack: nf_conntrack: table full, dropping packet
连接过多的解决方法两个:
(1) 加大nf_conntrack_max 值
vi /etc/sysctl.conf
net.nf_conntrack_max = 393216
net.netfilter.nf_conntrack_max = 393216
(2) 降低 nf_conntrack timeout时间
vi /etc/sysctl.conf
net.netfilter.nf_conntrack_tcp_timeout_established = 300
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
iptables -t nat -L -n
2.端口转发
[root@lanserver ~]# iptables -t nat -A PREROUTING -d 10.0.0.8 -p tcp --dport 80 -j REDIRECT --to-ports 8080