文件系统和日志分析
文件系统与日志分析
1. 文件系统
1.1 文件系统概念
文件数据都储存在"块"中,那么很显然,我们还必须找到一个地方储存文件的元信息,比如文件的创建者、文件的创建日期、文件的大小等等。这种储存文件元信息的区域就叫做inode,中文译名为"索引节点"。
inode表 ---> 存放文件的元信息
block ---> 存放文件的数据
1.2 inode是什么?
inode是一张表,它是存放文件的属性信息(文件的元信息),每个inode表也会消耗硬盘空间,每个inode的大小一般是128B或256B。格式化文件系统时,就确定了inode的数量,使用“df -i”命令可以查看每个硬盘分区的inode总数和已经使用的数量
文件的元信息,比如:文件的大小,时间,类型,权限等,这些信息称为文件的元数据(meta data 元信息)
inode表由很多条记录组成,每一条记录对应了一个文件的元信息
每一条记录包含以下信息:
inode节点号
文件类型(7大文件类型)
文件的权限
UID
GID
链接数(指向这个文件名的链接个数)
该文件的大小
该文件的时间戳
指向磁盘上文件数据块的指针
该文件的其他数据
1.3 查看硬盘的inode
[root@localhost test]# df -i
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/mapper/centos-root 20078592 149675 19928917 1% /
devtmpfs 229401 408 228993 1% /dev
tmpfs 233381 1 233380 1% /dev/shm
tmpfs 233381 592 232789 1% /run
tmpfs 233381 16 233365 1% /sys/fs/cgroup
/dev/sda1 524288 328 523960 1% /boot
/dev/mapper/centos-home 9801728 174 9801554 1% /home
tmpfs 233381 9 233372 1% /run/user/42
tmpfs 233381 1 233380 1% /run/user/0
[root@localhost test]#
1.4 如何查看文件的inode号
方法1. ls -i
[root@localhost test]# ll -i
总用量 4
33584399 -rw-r--r--. 1 root root 69 11月 3 20:41 t1
[root@localhost test]#
方法2. stat 文件名
[root@localhost test]# stat t1
文件:"t1"
大小:69 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:33584399 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2023-11-07 22:38:31.169053865 +0800
最近更改:2023-11-03 20:41:21.233295323 +0800
最近改动:2023-11-03 20:41:21.233295323 +0800
创建时间:-
[root@localhost test]#
# stat 可以查看文件的文件名、大小、block、占磁盘的大小、类型、inode号、硬链接数、权限、时间戳(文件的元信息)
# stat 查看文件的inode信息
1.5 inode号可能相同吗
同一个系统中,同一个磁盘或分区中的inode号肯定不相同,但是不同硬盘或分区的inode号可能相同
[root@localhost test]# find / -inum 50
/sys/devices/system/cpu/power/runtime_usage
/sys/kernel/debug/tracing/trace_stat/function10
[root@localhost test]#
# 可以看到,在同一个根下面,是有相同inode号的文件的
1.6 如何理解文件的时间戳
atime(access time) 最近访问时间 最后一次查看文件,只要打开文件
ctime(change time) 最近改动时间,最近更改文件元信息的时间,比如改变权限等
mtime(modify time) 最近更改文件内容的时间,更改了文件的内容(增删改)
[root@localhost test]# touch t2
[root@localhost test]# stat t2
文件:"t2"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:33584679 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2023-11-08 14:06:23.942766086 +0800
最近更改:2023-11-08 14:06:23.942766086 +0800
最近改动:2023-11-08 14:06:23.942766086 +0800
创建时间:-
[root@localhost test]#
# 改变atime,只要打开文件就会改变atime,其他时间不变
[root@localhost test]# cat t2
最近访问:2023-11-08 14:08:06.542771943 +0800 # atime
# 改变ctime,改变文件的元信息就会改变ctime,其他时间不变
[root@localhost test]# chown lisi:lisi t2
最近改动:2023-11-08 14:11:16.626635464 +0800 # ctime
# 改变mtime,修改文件的内容就会改变mtime,ctime也会改变,atime时间不变
[root@localhost test]# echo bb > t2
最近更改:2023-11-08 14:14:02.453261174 +0800 # mtime
最近改动:2023-11-08 14:14:02.453261174 +0800 # ctime
# 为什么改变了文件的内容,也会改变ctime呢?
因为文件的元信息中有一个文件大小,你改变了文件的内容,文件大小肯定也会改变
我们在用find查找文件的时间戳时,最好用mtime,因为我们最关心的还是文件的内容
1.7 block(块)
连续八个扇区(512B)组成一个block
块是文件存取的最小单位,系统读取数据是一块一块读数据的
扇区是文件存储的最小单位
注意
文件必须占用一个inode号,但是至少占用一个block(4K)
1.8 指针

mode 权限
owner 所有者
size 大小
直接指针(指针)
直接指针是指向实际数据
直接指针默认为12个,所以直接指针指向实际数据的最大数为4Kx12=48K
由于文件过大,直接指针无法满足48k的文件需求,所以出现了间接指针
间接指针
间接指针指向的不是实际数据,而是指向一个数据块,该数据块存放的是直接指针,每个指针为4B,所以指针数为4k/4=1024(个)
还有双重间接指针、三种间接指针
1.9 用户访问文件的过程
用户访问文件时
① 先去查找该文件的文件夹
② 再去到该文件夹中查找该文件的inode号
③ 然后通过inode号去查找inode表
④ 接着通过inode表去找对应的指针
⑤ 最后通过指针去找到对应的实际数据
1.10 inode的特殊性
由于inode号与文件名分离,会导致一些问题
当文件名包含特殊字符时,通过文件名是无法删除的,只能通过inode号去删除文件
1.10.1 cp 和 inode
cp 命令
- 分配一个空闲的inode号,在inode表中生成新条目
- 在目录中创建一个目录项,将名称与inode编号关联
- 拷贝数据生成新的文件
cp一个文件,看看cp与inode号的关系
[root@localhost test]# touch a
[root@localhost test]# cp a ./b
[root@localhost test]# ll -i
33561297 -rw-r--r-- 1 root root 0 11月 8 17:07 a
33561307 -rw-r--r-- 1 root root 0 11月 8 17:07 b
cp命令会生成新的inode号,a和b是两个不同的inode号
1.10.2 rm 和inode
rm命令
- 硬链接数递减,从而释放的inode号可以被重用
- 把数据块放在空闲列表中
- 删除目录项
- 数据实际上不会马上被删除,但当另一个文件使用数据块时将被覆盖
删除文件,看看rm与inode的关系
[root@localhost test]# rm -rf b
[root@localhost test]# ll -i
总用量 12
33561297 -rw-r--r-- 1 root root 0 11月 8 17:07 a
33584680 -rw-r--r-- 1 root root 2137 11月 7 19:42 passwd
33584399 -rw-r--r--. 1 root root 69 11月 3 20:41 t1
33584679 -rw-r--r-- 1 lisi lisi 3 11月 8 14:14 t2
# 目录中没有b文件了,b文件的inode号被释放,可以重用
# 删除文件,实际是删除了指针和元信息
1.10.3 mv和inode
mv命令
- 如果mv命令的目标和源在同一设备,
- 不影响inode表(除时间戳)或磁盘上的数据位置:没有数据被移动!
- 删除旧的目录对应关系,新建目录对应关系
mv文件,看看mv与inode的关系
# 1.源文件位置和移动的位置在同一个设备中
33561297 -rw-r--r-- 1 root root 0 11月 8 17:07 a
[root@localhost test]# mv a ../
33561297 -rw-r--r-- 1 root root 0 11月 8 17:07 a
# a文件的inode号不变,只是文件位置改变
# 2. 源文件位置和移动的位置不在同一个设备中
33561297 -rw-r--r-- 1 root root 0 11月 8 17:07 a
[root@localhost opt]# mv /opt/a /mnt/
67 -rw-r--r-- 1 root root 0 11月 8 17:07 a
# 我把a文件从sda硬盘移到sdb硬盘上,a的inode号发生了改变
删除乱码文件
[root@localhost t1]# ll -i
总用量 4
33561307 -rw-r--r-- 1 root root 61 11月 3 16:44 ♠√$你@_4f$%@▲.txt
[root@localhost t1]#
# 可以通过inode号删除文件
[root@localhost t1]# find /opt -inum 33561307 -delete
1.11 面试题
1.磁盘还有空间,但是怎么创建不了文件
inode号使用完了
磁盘的容量有磁盘的空间和inode号决定,只要其中一个满了,都创建不了文件
2. xfs类型文件的备份和恢复
CentOS 7 系统默认采用 xfs 类型的文件。针对 xfs 文件系统目前也没有比较成熟的文件恢复工具,所以建议提前做好数据备份,以避免数据丢失
xfs类型文件使用xfsdump与xfsrestore工具进行备份
2.1 安装工具
yum install -y xfsdump
yum install -y xfsrestore
2.2 xfsdump的备份级别有两种
0 完全备份
1-9 增量备份
xfsdump的默认备份级别为0
2.3 xfsdump的格式
xfsdump -f 备份存放的位置 要备份路径或设备文件
2.4 xfsdump的选项
-f:指定备份文件目录
-L:指定标签 session label
-M:指定设备标签 media label
-s:备份单个文件,-s 后面不能直接跟路径
2.5 xfsdump的注意事项
不支持没有挂载的文件系统备份,要想使用该命令,必须要先挂载
必须使用root权限才能备份
只能备份xfs类型的文件系统
备份下来的数据只能让xfsrestore来解析
不能备份两个具有相同UUID(硬件设备的ID)的文件系统
2.6 用xfsdump与xfsrestore工具实现数据的备份与恢复
[root@localhost mnt]# xfsdump -f /opt/sdc_1 /dev/sdc1
# 把/dev/sdc1备份到/opt/sdc_1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control
============================= dump label dialog ==============================
please enter label for this dump session (timeout in 300 sec)
-> /opt/sdc_1 # 第一个写备份到哪里,备份的位置
session label entered: "/opt/sdc_1"
--------------------------------- end dialog ---------------------------------
xfsdump: level 0 dump of localhost.localdomain:/mnt
xfsdump: dump date: Wed Nov 8 18:39:53 2023
xfsdump: session id: 95f34bae-11e3-4603-ae88-7d80706808ff
xfsdump: session label: "/opt/sdc_1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 10506880 bytes
xfsdump: /var/lib/xfsdump/inventory created
============================= media label dialog =============================
please enter label for media in drive 0 (timeout in 300 sec)
-> /dev/sdc1 # 把哪个设备备份
media label entered: "/dev/sdc1"
--------------------------------- end dialog ---------------------------------
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 10510624 bytes
xfsdump: dump size (non-dir files) : 10489288 bytes
xfsdump: dump complete: 31 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /opt/sdc_1 OK (success)
xfsdump: Dump Status: SUCCESS
[root@localhost mnt]# cd /opt
[root@localhost opt]# ls
sdc_1
以上已备份完毕
------------------------------------------------------
删除数据
[root@localhost opt]# cd /mnt
[root@localhost mnt]# ls
bigfile
[root@localhost mnt]# rm -rf * # 删除sdc1里面的文件
-----------------------------------------------------
使用xfsrestore恢复文件
[root@localhost mnt]# xfsrestore -f /opt/sdc_1 /mnt
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /mnt
xfsrestore: volume: /dev/sdc1
xfsrestore: session time: Wed Nov 8 18:39:53 2023
xfsrestore: level: 0
xfsrestore: session label: "/opt/sdc_1"
xfsrestore: media label: "/dev/sdc1"
xfsrestore: file system id: 35bd63ee-b4ec-4331-a373-1f93cd8fdee4
xfsrestore: session id: 95f34bae-11e3-4603-ae88-7d80706808ff
xfsrestore: media id: 1d0b7be1-4a87-4289-8517-072a399c8e50
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 1 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore: stream 0 /opt/sdc_1 OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost mnt]# cd /mnt
[root@localhost mnt]# ls
bigfile
[root@localhost mnt]#
# 文件已恢复成功
3. 日志管理
3.1 日志基本概念
日志保存位置
- linux系统中日志文件默认存放在:/var/log
- 内核及系统日志由系统服务rsyslog工具根据它主配置文件/etc/rsyslog.conf进行统一管理
日志的功能
- 用于记录系统中发生的各种事件
- 通过查看日志,可以帮助我们诊断和解决系统故障
日志文件的分类
- 内核及系统日志
- 由系统服务工具rsyslog统一管理,它们的日志格式基本相似
- 用户日志
- 记录系统用户登录和退出系统的相关信息
- 程序日志
- 由各种应用程序单独管理的日志文件,它们的记录格式不统一
3.2 日志文件
/var/log/messages
内核和公共日志,文本格式,可以直接查看
它是核心系统日志文件,其中包含了系统启动时的引导信息,以及系统运行时的其他状态消息。I/O 错误、网络错误和其他系统错误也都会记录到此文件中。
其他信息,比如某个人的身份切换为 root,已及用户自定义安装软件的日志,也会在这里列出。
[root@localhost log]# cat /var/log/messages
[root@localhost log]# tail -f /var/log/messages
Nov 8 19:14:20 localhost su: (to lisi) root on pts/1
# 倒序显示日志消息,最新的消息在最下面
Nov 8 日期
19:14:20 时间
localhost 主机
su 命令
/var/log/secure
系统安全日志,文本格式,可以直接查看
记录登录、验证、授权、账户和密码的信息,比如系统的登录、ssh的登录、su切换用户,sudo授权,甚至添加用户和修改用户密码都会记录在这个日志文件中
[root@localhost log]# tail -f /var/log/secure
[root@localhost log]# cat /var/log/secure
Nov 8 19:14:31 localhost su: pam_unix(su-l:session): session closed for user lisi
# 倒序显示日志消息,最新的消息在最下面
/var/log/btmp
用户登录失败的信息,二进制格式,只能用lastb命令进行查看
[root@localhost log]# file /var/log/btmp
/var/log/btmp: DBase 3 index file
[root@localhost log]# lastb # 使用lastb查看btmp的内容
lisi :0 :0 Wed Nov 8 19:31 - 19:31 (00:00)
root pts/1 Wed Nov 8 19:29 - 19:29 (00:00)
# 在GUI登录或者su切换用户登录时,只要登录失败,失败信息都会记录在/var/log/btmp这个日志文件中
# 该日志文件是最新的日志消息在最上面(逆序输出)
/var/log/wtmp
用户正常登录系统的相关日志信息,二进制格式,last命令进行查看
[root@localhost /]# last
lisi :0 :0 Wed Nov 8 20:53 still logged in
root pts/0 192.168.32.1 Wed Nov 8 20:52 still logged in
root pts/1 192.168.32.1 Wed Nov 8 19:08 - 19:57 (00:49)
# 该日志文件是最新的日志消息在最上面(逆序输出)
/var/log/lastlog
每一个用户最近一次的登录信息,二进制格式,lastlog命令进行查看
[root@localhost ~]# lastlog
用户名 端口 来自 最后登陆时间
root pts/1 192.168.32.1 三 11月 8 20:59:30 +0800 2023
bin **从未登录过**
wy **从未登录过**
lisi :0 三 11月 8 20:53:00 +0800 2023
/var/log/dmesg
CentOS 7 之前版本系统引导过程中的日志信息,文本格式,在开机时收集硬件信息,硬件是否正常
[root@localhost ~]# vim /var/log/dmesg
[ 0.000000] ACPI: LAPIC (acpi_id[0x30] lapic_id[0x60] disabled)
[ 0.000000] ACPI: LAPIC (acpi_id[0x31] lapic_id[0x62] disabled)
/var/log/boot.log
系统服务启动的相关信息,文本格式
[root@localhost log]# cat boot.log-20231103
[ OK ] Started Show Plymouth Boot Screen.
[ OK ] Reached target Paths.
[ OK ] Reached target Basic System.
[ OK ] Found device /dev/mapper/centos-root.
/var/log/anaconda/
安装系统时的软件信息
[root@localhost log]# cd /var/log/anaconda/
anaconda.log journal.log ks-script-I6PKbM.log program.log syslog
ifcfg.log ks-script-3sCMaO.log packaging.log storage.log X.log
[root@localhost anaconda]# cat anaconda.log
---------------------------------------------------
00:26:58,294 INFO anaconda: 正在安装引导装载程序
00:26:58,295 INFO anaconda: 正在执行安装后设置
00:26:58,319 INFO anaconda: 正在执行安装后设置
00:26:58,342 INFO anaconda: Thread Done: AnaInstallThread (139754509797120)
00:26:58,844 INFO anaconda: Running Thread: AnaConfigurationThread (139754509797120)
00:26:58,846 INFO anaconda: 正在配置已安装系统
00:27:01,589 INFO anaconda: 正在配置已安装系统
00:27:01,591 INFO anaconda: 正写入网络配置
00:27:01,992 DEBUG anaconda: Could not get default inet route device
00:27:02,017 DEBUG anaconda: Could not get default inet6 route device
00:27:02,027 INFO anaconda: 正写入网络配置
00:27:02,027 INFO anaconda: 创建用户
00:27:02,027 INFO anaconda: Clearing libuser.conf at /tmp/libuser.A5FkPL
00:27:03,337 INFO anaconda: 创建用户
00:27:03,340 INFO anaconda: 正在配置附加组件
00:27:03,369 INFO anaconda: 正在配置附加组件
00:27:03,370 INFO anaconda: 正在生成 initramfs
00:28:55,005 INFO anaconda: 正在生成 initramfs
00:28:55,007 INFO anaconda: 正在运行安装后脚本
4. rsyslog管理
提供日志管理功能
查看rsyslog软件的配置文件
rpm -qc rsyslog
[root@localhost /]# rpm -qc rsyslog
/etc/logrotate.d/syslog
/etc/rsyslog.conf
/etc/sysconfig/rsyslog
[root@localhost /]#
查看rsyslog软件的列表
rpm -ql rsyslog
[root@localhost /]# rpm -ql rsyslog
/usr/lib64/rsyslog/imtcp.so
/usr/lib64/rsyslog/imudp.so
/usr/lib64/rsyslog/mmutf8fix.so
# ".so" 结尾的是它的模块
rsyslog特性
自定义输出格式
多线程
支持的协议多
适用于小型企业
日志级别
debug 调试信息
info 基本的通知信息
notice 普通信息,但是有一定的重要性
warning 警告信息,但是还不会影响到服务或系统的运行
error 错误信息,一般达到error登记的信息已经可以影响到服务或系统的运行了
crit 临界状况,比error等级要严重
alert 状态信息,还严重了,必须立即采取行动
emerg 疼痛等级信息,系统已经无法使用了
* 代表所有日志等级
认识rsyslog.conf配置文件
# Don't log private authentication messages!
[root@localhost /]# vim /etc/rsyslog.conf
-----------------------------------------------------
*.info;mail.none;authpriv.none;cron.none /var/log/messages
*.info info及info以上的日志级别记录下来
mail.none 不包含mail的日志
# Log all the mail messages in one place.
[root@localhost /]# vim /etc/rsyslog.conf
-----------------------------------------------------
mail.* -/var/log/maillog
日志首先产生在内存中
- 表示异步写入磁盘,过一段时间再写入日志
同步写入磁盘,有就写,应用于银行,需要保证一条信息都不遗漏
实验
1. 利用local自定义日志,实现将ssh的日志独立出来
# Save boot messages also to boot.log
[root@localhost /]# vim /etc/rsyslog.conf
-----------------------------------------------------
local7.* /var/log/boot.log
local1-7.*
可以自定义,这里有7,所以还可以写1-6
第一步
关闭防火墙
关闭核心防护
第二步
# 因为ssh没有单独的日志文件,我们使用local自定义日志文件
[root@localhost /]# vim /etc/ssh/sshd_config
------------------------------------------------
#SyslogFacility AUTHPRIV
SyslogFacility local6
# 修改 AUTHPRIV 为我们自定义的 local6
第三步
[root@localhost /]# vim /etc/rsyslog.conf
-----------------------------------------------------
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local6.* /opt/rsyslog.log
第四步
[root@localhost opt]# systemctl restart sshd rsyslog.service
[root@localhost opt]# ls
rsyslog.log
# 重启ssh和rsyslog.service服务后,会在指定目录下生成日志文件
Nov 8 21:55:54 localhost sshd[7769]: Server listening on 0.0.0.0 port 22.
Nov 8 21:55:54 localhost sshd[7769]: Server listening on :: port 22.
Nov 8 21:59:15 localhost sshd[7863]: Connection closed by 192.168.32.11 port 44324 [preauth]
Nov 8 21:59:51 localhost sshd[7875]: Accepted password for root from 192.168.32.11 port 44328 ssh2
[root@localhost opt]#
2. 网络日志(远程日志功能)
第一步
关闭防火墙
关闭核心防护
第二步:服务器(192.168.32.11)上面开启远程传输功能,服务器作为日志服务器,只需要开启端口
[root@localhost ~]# vim /etc/rsyslog.conf
------------------------------------------------
14 # Provides UDP syslog reception
15 #$ModLoad imudp
16 #$UDPServerRun 514
17
18 # Provides TCP syslog reception
19 $ModLoad imtcp
20 $InputTCPServerRun 514
# udp、tcp随便开启哪一个都习惯,udp快,tcp稳定
# 如果是外网,用tcp,如果是内网,用udp
@@ 表示使用tcp协议,@@192.168.32.11
@ 表示使用udp协议,@192.168.32.11
也可以使用其他没有用过的端口号 @@192.168.32.11:9527 端口号必须配置成一样的,$InputTCPServerRun 9527
---------------------------------------------
[root@localhost ~]# systemctl restart rsyslog.service
修改完配置文件,重启rsyslog.service服务
第三步:客户机(192.168.32.12)上面也开启远程传输功能;不仅开启端口,还要修改日志传输到服务器上面
[root@localhost ~]# hostname test12
[root@localhost ~]# su
[root@test12 ~]#
# 更改主机名,为了方便识别这个客户机
[root@test12 ~]# vim /etc/rsyslog.conf
----------------------------------------------
18 # Provides TCP syslog reception
19 $ModLoad imtcp
20 $InputTCPServerRun 514
52 # Log anything (except mail) of level info or higher.
53 # Don't log private authentication messages!
54 *.info;mail.none;authpriv.none;cron.none /var/log/messages
55 *.info;mail.none;authpriv.none;cron.none @@192.168.32.11
---------------------------------------------------
[root@test12 ~]# systemctl restart rsyslog.service
修改完配置文件,重启rsyslog.service服务
[root@test12 ~]# logger "this is test log from 192.168.32.12"
# 测试命令,看看本机日志和日志服务器上的日志是否更新
这是本机日志
Nov 8 22:34:03 test12 root: this is test log from 192.168.32.12
日志服务器的日志
Nov 8 22:34:03 test12 root: this is test log from 192.168.32.12
3. 配置文件写错,怎么排查
以nginx为例
[root@localhost yum.repos.d]# nginx -t
nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost yum.repos.d]#
# nginx启动不起来,根据提示进行排查
方法1:systemctl status nginx.service
systemctl status nginx.service
---------------------------------------------------------------------------------------------------
[root@localhost yum.repos.d]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 三 2023-11-08 23:00:18 CST; 1min 9s ago
Process: 1955 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
Process: 1952 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
11月 08 23:00:18 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: configuration file /etc/nginx/nginx.conf test failed
11月 08 23:00:18 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
11月 08 23:00:18 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
11月 08 23:00:18 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
11月 08 23:00:18 localhost.localdomain systemd[1]: nginx.service failed.
[root@localhost yum.repos.d]#
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
提示说:在nginx.conf这个配置文件中的第五行有一个关键词(directive)"r"
所以,我们vim +5 /etc/nginx/nginx.conf到第五行进行修改
r nginx;
user nginx;
[root@localhost /]# nginx -t # 检测配置文件是否有错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost /]#
# 配置文件测试成功,没有错误
方法2:journalctl 日志管理工具,用来监测所有被systemctl管理的程序,并形成日志文件
journalctl -xe
----------------------------------------------------------------------------------------------------
[root@localhost yum.repos.d]# journalctl -xe --no-pager
-- Unit nginx.service has begun starting up.
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: configuration file /etc/nginx/nginx.conf test failed
11月 08 23:00:18 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
11月 08 23:00:18 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
11月 08 23:00:18 localhost.localdomain nginx[1955]: nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
提示说:在nginx.conf这个配置文件中的第五行有一个关键词(directive)"r"
所以,我们vim +5 /etc/nginx/nginx.conf到第五行进行修改
r nginx;
user nginx;
[root@localhost /]# nginx -t # 检测配置文件是否有错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost /]#
# 配置文件测试成功,没有错误
方法3:nginx -t 检查是否有错误
nginx -t 检查是否有错误
---------------------------------------------------------------------------------------------------
[root@localhost /]# nginx -t
nginx: [emerg] unknown directive "r" in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@localhost /]#
提示说:在nginx.conf这个配置文件中的第五行有一个关键词(directive)"r"
所以,我们vim +5 /etc/nginx/nginx.conf到第五行进行修改
r nginx;
user nginx;
[root@localhost /]# nginx -t # 检测配置文件是否有错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost /]#
# 配置文件测试成功,没有错误
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律