Linux 日志管理
rsyslog 日志配置
最佳实践
- 故障1:sshd_config文件丢失
模拟:
[root@centos7 ~ 12:32:43]$ mv /etc/ssh/sshd_config .
[root@centos7 ~ 12:33:02]$ systemctl restart sshd
监控日志:
[root@centos7 ~ 12:33:21]$ tail -f /var/log/messages
Jul 25 12:33:21 centos7 systemd: Stopped OpenSSH server daemon.
Jul 25 12:33:21 centos7 systemd: Starting OpenSSH server daemon...
#######################################################################################
Jul 25 12:33:21 centos7 sshd: /etc/ssh/sshd_config: No such file or directory
#######################################################################################
Jul 25 12:33:21 centos7 systemd: sshd.service: main process exited, code=exited, status=1/FAILURE
Jul 25 12:33:21 centos7 systemd: Failed to start OpenSSH server daemon.
Jul 25 12:33:21 centos7 systemd: Unit sshd.service entered failed state.
Jul 25 12:33:21 centos7 systemd: sshd.service failed.
根据提示恢复文件。
[root@centos7 ~ 12:33:39]$ mv sshd_config /etc/ssh/
[root@centos7 ~ 12:33:58]$ systemctl restart sshd
- 故障2:sshd_config配置错误
模拟故障:echo hello world >> /etc/ssh/sshd_config
[root@centos7 ~ 12:34:17]$ echo hello world >> /etc/ssh/sshd_config
监控日志:
[root@centos7 ~ 12:34:36]$ tail -f /var/log/messages
Jul 25 12:34:36 centos7 systemd: Stopping OpenSSH server daemon...
Jul 25 12:34:36 centos7 systemd: Stopped OpenSSH server daemon.
Jul 25 12:34:36 centos7 systemd: Starting OpenSSH server daemon...
#######################################################################################
Jul 25 12:34:36 centos7 sshd: /etc/ssh/sshd_config: line 144: Bad configuration option: hello
Jul 25 12:34:36 centos7 sshd: /etc/ssh/sshd_config: terminating, 1 bad configuration options
#######################################################################################
Jul 25 12:34:36 centos7 systemd: sshd.service: main process exited, code=exited, status=255/n/a
Jul 25 12:34:36 centos7 systemd: Failed to start OpenSSH server daemon.
Jul 25 12:34:36 centos7 systemd: Unit sshd.service entered failed state.
Jul 25 12:34:36 centos7 systemd: sshd.service failed.
根据提示修复配置文件错误。
补充
-
虽然系统提供了日志服务,但并不会记录所有内容。
-
系统中的应用程序是否使用 rsyslog 服务记录日志,取决于应用程序设计。
例如:
-
httpd 服务使用自己的日志记录。
-
sshd 服务使用 rsyslog 服务记录登录和退出日志。
[root@centos7 ~ 12:35:08]$ grep AUTHPRIV /etc/ssh/sshd_config
SyslogFacility AUTHPRIV
[root@centos7 ~ 12:35:27]$ grep ^authpri /etc/rsyslog.conf
authpriv.* /var/log/secure
[root@centos7 ~ 12:35:46]$ tail -1 /var/log/secure
Jul 25 14:10:39 centos7 sshd[20527]: pam_unix(sshd:session): session opened for user root by (uid=0)
systemd-journald 日志
systemd-journald 日志持久化保存
systemd-journald 配置文件 /etc/systemd/journald.conf
详情参考 journald.conf(5)
Storage 参数 ,更改参数值后需要重启systemd-journald服务。
-
persistent :将日志存储在/var/log/journal目录中,这可在系统重启后持久保留。如果/var/log/journal目录不存在,systemd-journald服务会创建它。
-
volatile :将日志存储在易失性/run/log/journal目录中。因为/run文件系统是临时的,仅存在于运行时内存中,存储在其中的数据(包括系统日志)不会在系统启后持久保留。
-
auto :如果/var/log/journal目录存在,那么rsyslog会使用持久存储,否则使用易失性存储。如果未设置Storage参数,此为默认操作。
[root@centos7 ~ 12:36:05]$ vim /etc/systemd/journald.conf
......
[Journal]
# Storage=auto
Storage=persistent
......
[root@centos7 ~ 12:36:24]$ systemctl restart systemd-journald
systemd-journald 日志分析
[root@centos7 ~ 12:36:43]$ ls /var/log/journal/
b8b0960cabe3452ca432f45acb1df028
# systemd-journald日志是以二进制方式存储,不能使用常规文本工具查看
# 查看所有日志条目,默认使用less查看文档
[root@centos7 ~ 12:37:02]$ journalctl
# 动态查看所有日志条目
[root@centos7 ~ 12:37:21]$ journalctl -f
# 查看本次系统启动到现在所有日志条目
[root@centos7 ~ 12:37:40]$ journalctl -b 0
# 1 第1次启动的日志
# 2 第2次启动的日志
# N 第n次启动的日志
# 查看最近5条日志
[root@centos7 ~ 12:37:59]$ journalctl -n 5
# 查看error级别日志
[root@centos7 ~ 12:38:18]$ journalctl -p err
[root@centos7 ~ 12:38:37]$ journalctl -p err | cat
# 根据时间查看日志
[root@centos7 ~ 12:38:56]$ journalctl --since today
[root@centos7 ~ 12:39:15]$ journalctl --since "2026-07-25 14:30:00" --until "2026-07-25 15:00:00"
[root@centos7 ~ 12:39:34]$ journalctl --since "-1 hour"
# 详细查看所有日志
[root@centos7 ~ 12:39:53]$ journalctl -o verbose
# 查看特定unit日志
[root@centos7 ~ 12:40:12]$ journalctl -u sshd.service
最佳实践
故障1:配置文件丢失
[root@centos7 ~ 12:40:31]$ mv /etc/ssh/sshd_config .
[root@centos7 ~ 12:40:50]$ systemctl restart sshd
处理过程 :通过日志发现 /etc/ssh/sshd_config: No such file or directory,文件丢失。
# 重启服务时,动态监控日志
[root@centos7 ~ 12:41:09]$ journalctl -f
7月 25 12:41:09 centos7 systemd[1]: Starting OpenSSH server daemon...
7月 25 12:41:09 centos7 sshd[2045]: /etc/ssh/sshd_config: No such file or directory
7月 25 12:41:09 centos7 systemd[1]: sshd.service: main process exited, code=exited, status=1/
7月 25 12:41:10 centos7 systemd[1]: Failed to start OpenSSH server
......
# 移动回来,并重启服务
[root@centos7 ~ 12:41:29]$ mv sshd_config /etc/ssh/sshd_config
[root@centos7 ~ 12:41:48]$ systemctl restart sshd
故障2:配置文件参数错误
[root@centos7 ~ 12:42:07]$ echo 'PermitRootLogin hahaha' >> /etc/ssh/sshd_config
[root@centos7 ~ 12:42:26]$ systemctl restart sshd
处理过程 :通过日志发现/etc/ssh/sshd_config line 141: unsupported option "hahaha".
# 重启服务时,动态监控日志
[root@centos7 ~ 12:42:45]$ journalctl -f
7月 25 12:42:45 centos7 systemd[1]: Stopped OpenSSH server daemon.
7月 25 12:42:45 centos7 systemd[1]: Starting OpenSSH server daemon...
7月 25 12:42:45 centos7 sshd[2806]: /etc/ssh/sshd_config line 141: unsupported option "hahaha".
7月 25 12:42:45 centos7 systemd[1]: sshd.service: main process exited, code=exited, status=25
7月 25 12:42:45 centos7 systemd[1]: Failed to start OpenSSH server daemon.
7月 25 12:42:45 centos7 systemd[1]: Unit sshd.service entered failed state.
# 清理对应无效记录,并重启服务
[root@centos7 ~ 12:43:04]$ sed -i '/hahaha/d' /etc/ssh/sshd_config
[root@centos7 ~ 12:43:23]$ systemctl restart sshd
故障3:配置文件参数错误
[root@centos7 ~ 12:43:42]$ yum install -y httpd
[root@centos7 ~ 12:44:01]$ sed -i 's/Listen 80/Listen 80000/g' /etc/httpd/conf/httpd.conf
[root@centos7 ~ 12:44:20]$ systemctl restart httpd
处理过程 :通过日志发现 AH00526: Syntax error on line 42 of /etc/httpd/conf/httpd.conf
# 重启服务时,动态监控日志
[root@centos7 ~ 12:44:39]$ journalctl -f
7月 25 12:44:39 centos7 systemd[1]: Starting The Apache HTTP Server...
7月 25 12:44:39 centos7 httpd[2877]: AH00526: Syntax error on line 42 of /etc/httpd/conf/httpd.conf:
7月 25 12:44:39 centos7 httpd[2877]: Invalid address or port
7月 25 12:44:39 centos7 systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
7月 25 12:44:39 centos7 systemd[1]: Failed to start The Apache HTTP Server.
7月 25 12:44:39 centos7 systemd[1]: Unit httpd.service entered failed state.
# 修改回来,并重启服务
[root@centos7 ~ 12:44:58]$ sed -i 's/Listen 80000/Listen 80/g' /etc/httpd/conf/httpd.conf
[root@centos7 ~ 12:45:17]$ systemctl restart httpd
Linux 时间管理
系统时间设置
date 命令
# 设置语言为英语
[root@centos7 ~ 12:45:36]$ LANG=en_US.utf8 date
Sat Jul 25 12:45:36 CST 2026
# 设置为特定时间,时间字符串必须是英文格式
[root@centos7 ~ 12:45:55]$ date -s '2026年 07月 25日 星期六 12:45:50 CST'
date: 无效的日期"2026年 07月 25日 星期六 12:45:50 CST"
[root@centos7 ~ 12:46:14]$ date -s 'Sat Jul 25 12:45:50 CST 2026'
2026年 07月 25日 星期六 12:45:50 CST
hwclock 命令
# 读取硬件时钟
[root@centos7 ~ 12:46:33]$ hwclock -r
2026年07月25日 星期六 12时46分33秒 -0.320836 秒
# 将硬件时钟时间设置与系统时间一致
[root@centos7 ~ 12:46:52]$ hwclock -w
# 将系统时间设置与硬件时钟时间一致
[root@centos7 ~ 12:47:11]$ hwclock -s
timedatectl 命令
[root@centos7 ~ 12:47:30]$ timedatectl
Local time: 六 2026-07-25 12:47:30 CST
Universal time: 六 2026-07-25 04:47:30 UTC
RTC time: 六 2026-07-25 04:47:37
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# RTC real time clock,也就是硬件时钟时间。
# NTP enabled: yes,代表对时服务chronyd应开机自启。
# 关闭自动对时
[root@centos7 ~ 12:47:49]$ timedatectl set-ntp no
[root@centos7 ~ 12:48:08]$ timedatectl
Local time: 六 2026-07-25 12:48:08 CST
Universal time: 六 2026-07-25 04:48:08 UTC
RTC time: 六 2026-07-25 04:48:08
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
[root@centos7 ~ 12:48:27]$ timedatectl set-time '2026-07-25 12:48:24'
# 如果自动对时未关闭,显示如下
[root@centos7 ~ 12:48:46]$ timedatectl set-time '2026-07-25 12:48:24'
Failed to set time: Automatic time synchronization is enabled
# 设置时区
[root@centos7 ~ 12:49:05]$ timedatectl set-timezone Asia/Shanghai
tzselect 命令
查询时区名称。
[root@centos7 ~ 12:49:24]$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Sat Jul 25 12:49:24 CST 2026.
Universal Time is now: Sat Jul 25 04:49:24 UTC 2026.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /bin/tzselect command in shell scripts:
Asia/Shanghai
windows 自动对时

自动对时-chronyd 服务
# 安装软件包
[root@centos7 ~ 12:49:43]$ yum install chrony
# 修改对时服务器
[root@centos7 ~ 12:50:02]$ vim /etc/chrony.conf
# 与时间池对时
# 时间池是包含多个时间服务器的服务器组
pool 2.rocky.pool.ntp.org iburst
# 与单个服务器 ntp.aliyun.com 对时
server ntp.aliyun.com iburst
# 启用并启动chronyd服务
[root@centos7 ~ 12:50:21]$ systemctl enable chronyd --now
# 如果之前已经启动,需要重启
[root@centos7 ~ 12:50:40]$ systemctl restart chronyd
# 验证对时情况
[root@centos7 ~ 12:50:59]$ chronyc sources -v
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 77 9 -1840us[-4378us] +/- 23ms
补充:vim 高级用法
vim 高级用法,可视化模式: 功能1:批量增加内容,例如注释多行内容
-
光标定位到目标位置,ctrl+v
-
向下移动光标,按需选中多行
-
I 进行插入,写入#
-
esc
功能2:列删除,例如批量解除注释 1. 光标定位到目标位置,ctrl+v 2. 向下移动光标,按需选中多行 3. x或d
功能3:列复制并粘贴,例如批量增加主机名后缀 1. 光标定位到目标位置,ctrl+v 2. 向下移动光标(可以上下左右同时移动,选冲矩形块),按需选中 3. y复制 4. 光标定位到目标位置,p粘贴
部署时间服务器
chrony 既可以作为客户端,也可以作为服务端(为客户端提供对时服务)。
服务端
[root@centos7 ~ 12:51:18]$ vim /etc/chrony.conf
# 最后添加两条记录
# 配置监听地址
bindaddress 10.1.8.10
# 配置允许哪些网段主机同步
allow 10.1.8.0/24
[root@centos7 ~ 12:51:37]$ systemctl restart chronyd
# 停止防火墙服务
[root@centos7 ~ 12:51:56]$ systemctl stop firewalld.service
必须确保服务端时间的准确性、有效性。
客户端
# 修改对时服务器
[root@client ~ 12:52:15]$ vim /etc/chrony.conf
# 与单个服务器 10.1.8.10 对时
server 10.1.8.10 iburst
[root@client ~ 12:52:34]$ systemctl restart chronyd
[root@client ~ 12:52:53]$ chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* server.laoma.cloud 3 6 7 1 -10us[ +43ms] +/- 76ms
Linux 网络管理
网络基础回顾
OSI 模型
-
物理层:提供为建立、维护和拆除物理链路所需要的机械的、电气的、功能的和规程的特性;有关的物理链路上传输非结构的位流以及故障检测指示。
-
数据链路层:在网络层实体间提供数据发送和接收的功能和过程;提供数据链路的流控。
-
网络层:控制分组传送系统的操作、路由选择、拥护控制、网络互连等功能,它的作用是将具体的物理传送对高层透明。
-
传输层:提供建立、维护和拆除传送连接的功能;选择网络层提供最合适的服务;在系统之间提供可靠的透明的数据传送,提供端到端的错误恢复和流量控制。
-
会话层:提供两进程之间建立、维护和结束会话连接的功能;提供交互会话的管理功能,如三种数据流方向的控制,即一路交互、两路交替和两路同时会话模式 。
-
表示层:代表应用进程协商数据表示;负责数据转换格式和数据加密、解密、压缩、解压等。
-
应用层:为用户的应用程序提供网络服务接口,包括文件传输、电子邮件、远程登录等。
TCP/IP 模型
域名系统,用于将域名解析为IP地址。
# 查看DNS配置
[root@centos7 ~ 12:54:05]$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.50.2
nameserver 8.8.8.8
# 测试DNS解析
[root@centos7 ~ 12:54:21]$ nslookup www.baidu.com
Server: 192.168.50.2
Address: 192.168.50.2#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 110.242.68.3
# 使用dig查询
[root@centos7 ~ 12:54:37]$ dig www.baidu.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3140
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 1
;; ANSWER SECTION:
www.baidu.com. 686 IN CNAME www.a.shifen.com.
www.a.shifen.com. 600 IN A 110.242.68.3
;; Query time: 2 msec
;; SERVER: 192.168.50.2#53(192.168.50.2)
;; WHEN: 六 7月 25 12:54:37 CST 2026
;; MSG SIZE rcvd: 132
网络连通性测试
# ping命令测试网络连通性
[root@centos7 ~ 12:54:53]$ ping -c 4 www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=53 time=12.3 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=53 time=11.8 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=3 ttl=53 time=12.1 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=4 ttl=53 time=11.9 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 11.845/12.053/12.342/0.217 ms
# traceroute追踪数据包路径
[root@centos7 ~ 12:55:09]$ traceroute www.baidu.com
traceroute to www.baidu.com (110.242.68.3), 30 hops max, 60 byte packets
1 gateway (192.168.50.2) 1.123 ms 1.045 ms 1.012 ms
2 * * *
3 * * *
......
# netstat或ss查看端口
[root@centos7 ~ 12:55:25]$ ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 127.0.0.1:25 0.0.0.0:*
# 使用curl测试HTTP服务
[root@centos7 ~ 12:55:41]$ curl -I http://www.baidu.com
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Server: BWS/1.1
......
# 使用wget下载文件
[root@centos7 ~ 12:55:57]$ wget http://www.baidu.com
--2026-07-25 12:55:57-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 110.242.68.3
Connecting to www.baidu.com (www.baidu.com)|110.242.68.3|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: 'index.html'
配置网络
环境准备
# 查看网络接口
[root@centos7 ~ 12:56:13]$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:00:00:01 brd ff:ff:ff:ff:ff:ff
# 查看IP地址
[root@centos7 ~ 12:56:29]$ ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:00:00:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.50.100/24 brd 192.168.50.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe00:1/64 scope link
valid_lft forever preferred_lft forever
# 查看网卡驱动
[root@centos7 ~ 12:56:45]$ ethtool ens33
Settings for ens33:
Link detected: yes
Speed: 1000Mb/s
Duplex: Full
Auto-negotiation: on
nmtui 命令
nmtui是NetworkManager的文本界面前端工具,用于交互式配置网络。
# 启动nmtui
[root@centos7 ~ 12:57:01]$ nmtui
nmtui提供三个选项:
-
Edit a connection:编辑连接
-
Activate a connection:激活连接
-
Set system hostname:设置系统主机名
操作步骤:
-
使用Tab键切换到"Edit a connection"
-
选择网络接口(如ens33)
-
编辑网络参数(IP地址、子网掩码、网关、DNS等)
-
点击"OK"保存配置
-
返回主菜单,选择"Activate a connection"重新激活连接
nmcli 命令
nmcli是NetworkManager的命令行工具,用于配置网络。
# 查看所有连接
[root@centos7 ~ 12:57:17]$ nmcli connection show
NAME UUID TYPE DEVICE
ens33 5fb06bd4-3bcc-4ebb-85b5-3dfa3f555555 ethernet ens33
# 查看特定连接详情
[root@centos7 ~ 12:57:33]$ nmcli connection show ens33
connection.id: ens33
connection.uuid: 5fb06bd4-3bcc-4ebb-85b5-3dfa3f555555
connection.interface-name: ens33
connection.type: ethernet
......
# 查看活动连接
[root@centos7 ~ 12:57:49]$ nmcli connection show --active
NAME UUID TYPE DEVICE
ens33 5fb06bd4-3bcc-4ebb-85b5-3dfa3f555555 ethernet ens33
# 创建新的连接
[root@centos7 ~ 12:58:05]$ nmcli connection add type ethernet con-name ens34 ifname ens34
Connection 'ens34' (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) successfully added.
# 设置IP地址
[root@centos7 ~ 12:58:21]$ nmcli connection modify ens34 ipv4.addresses 192.168.51.100/24
[root@centos7 ~ 12:58:37]$ nmcli connection modify ens34 ipv4.gateway 192.168.51.1
[root@centos7 ~ 12:58:53]$ nmcli connection modify ens34 ipv4.dns 192.168.51.1
# 方法:man nmcli
# 设置为手动获取IP
[root@centos7 ~ 12:59:09]$ nmcli connection modify ens34 ipv4.method manual
# 设置为自动获取IP
[root@centos7 ~ 12:59:25]$ nmcli connection modify ens34 ipv4.method auto
# 启动连接
[root@centos7 ~ 12:59:41]$ nmcli connection up ens34
# 断开连接
[root@centos7 ~ 12:59:57]$ nmcli connection down ens34
# 删除连接
[root@centos7 ~ 13:00:13]$ nmcli connection delete ens34
# 重新加载配置
[root@centos7 ~ 13:00:29]$ nmcli connection reload
网络配置文件
网络配置文件通常位于 /etc/sysconfig/network-scripts/ 目录下。
# 查看网络配置文件
[root@centos7 ~ 13:00:45]$ ls /etc/sysconfig/network-scripts/
ifcfg-ens33
# 查看配置文件内容
[root@centos7 ~ 13:01:01]$ cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.50.100"
PREFIX="24"
GATEWAY="192.168.50.2"
DNS1="192.168.50.2"
DNS2="8.8.8.8"
# 修改配置文件后重启网络服务
[root@centos7 ~ 13:01:17]$ nmcli connection reload
[root@centos7 ~ 13:01:33]$ nmcli connection up ens33
配置文件参数说明:
| 参数 | 说明 |
|---|---|
| TYPE | 网络类型,如Ethernet |
| BOOTPROTO | 获取IP方式,none/static/dhcp |
| NAME | 连接名称 |
| DEVICE | 设备名称 |
| ONBOOT | 是否开机自启 |
| IPADDR | IP地址 |
| NETMASK | 子网掩码 |
| GATEWAY | 网关 |
| DNS1/DNS2 | DNS服务器 |
| PREFIX | 前缀长度(替代NETMASK) |
配置主机名和名称解析
# 查看当前主机名
[root@centos7 ~ 13:01:49]$ hostname
centos7
# 查看主机名配置
[root@centos7 ~ 13:02:05]$ hostnamectl
Static hostname: centos7
Pretty hostname: centos7
Icon name: computer-vm
Chassis: vm
State: running
Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Boot ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7:kernel
Kernel: Linux 3.10.0-862.el7.x86_64
# 临时修改主机名
[root@centos7 ~ 13:02:21]$ hostname newname
[root@centos7 ~ 13:02:37]$ hostname
newname
# 永久修改主机名
[root@centos7 ~ 13:02:53]$ hostnamectl set-hostname newname
[root@centos7 ~ 13:03:09]$ hostnamectl
Static hostname: newname
Pretty hostname: newname
Icon name: computer-vm
Chassis: vm
State: running
......
# 配置hosts文件
[root@centos7 ~ 13:03:25]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 添加主机名解析
[root@centos7 ~ 13:03:41]$ echo '192.168.50.100 centos7' >> /etc/hosts
# 测试名称解析
[root@centos7 ~ 13:03:57]$ ping -c 2 centos7
PING centos7 (192.168.50.100) 56(84) bytes of data.
64 bytes from centos7 (192.168.50.100): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from centos7 (192.168.50.100): icmp_seq=2 ttl=64 time=0.048 ms
--- centos7 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
# 查看hostname配置文件
[root@centos7 ~ 13:04:13]$ cat /etc/hostname
centos7
课后练习
- 配置静态IP地址:
# 创建新连接
[root@centos7 ~ 13:04:29]$ nmcli connection add type ethernet con-name static-con ifname ens33
# 设置IP地址和网关
[root@centos7 ~ 13:04:45]$ nmcli connection modify static-con ipv4.addresses 192.168.50.200/24
[root@centos7 ~ 13:05:01]$ nmcli connection modify static-con ipv4.gateway 192.168.50.2
[root@centos7 ~ 13:05:17]$ nmcli connection modify static-con ipv4.dns 8.8.8.8
# 设置为手动获取
[root@centos7 ~ 13:05:33]$ nmcli connection modify static-con ipv4.method manual
# 启动连接
[root@centos7 ~ 13:05:49]$ nmcli connection up static-con
# 验证配置
[root@centos7 ~ 13:06:05]$ ip addr show ens33
[root@centos7 ~ 13:06:21]$ nmcli connection show --active
- 测试网络连通性:
# 测试网关连通性
[root@centos7 ~ 13:06:37]$ ping -c 4 192.168.50.2
# 测试外网连通性
[root@centos7 ~ 13:06:53]$ ping -c 4 www.baidu.com
# 测试端口连通性
[root@centos7 ~ 13:07:09]$ curl -I http://www.baidu.com
:
```bash
# 创建新连接
[root@centos7 ~ 13:04:29]$ nmcli connection add type ethernet con-name static-con ifname ens33
# 设置IP地址和网关
[root@centos7 ~ 13:04:45]$ nmcli connection modify static-con ipv4.addresses 192.168.50.200/24
[root@centos7 ~ 13:05:01]$ nmcli connection modify static-con ipv4.gateway 192.168.50.2
[root@centos7 ~ 13:05:17]$ nmcli connection modify static-con ipv4.dns 8.8.8.8
# 设置为手动获取
[root@centos7 ~ 13:05:33]$ nmcli connection modify static-con ipv4.method manual
# 启动连接
[root@centos7 ~ 13:05:49]$ nmcli connection up static-con
# 验证配置
[root@centos7 ~ 13:06:05]$ ip addr show ens33
[root@centos7 ~ 13:06:21]$ nmcli connection show --active
- 测试网络连通性:
# 测试网关连通性
[root@centos7 ~ 13:06:37]$ ping -c 4 192.168.50.2
# 测试外网连通性
[root@centos7 ~ 13:06:53]$ ping -c 4 www.baidu.com
# 测试端口连通性
[root@centos7 ~ 13:07:09]$ curl -I http://www.baidu.com

586

被折叠的 条评论
为什么被折叠?



