https://access.redhat.com/solutions/2985791
SOLUTION 已验证
已验证此解决方案可由红帽客户和支持工程师用于指定的产品版本。
- 已更新 2018年十月10日03:27 -
环境
- Red Hat Enterprise Linux 7
- systemd
问题
- Hostname was changed but
systemctl status
command for services shows old hostname. - Why does services related logs shows old hostname even after change of hostname ?
- Hostname was changed from
node1.example.com
tonode2.example.com
butsystemctl status
command for few services still shows hostname asnode1.example.com
.
决议
- Check the timestamp of the logs, if service related logs in
systemctl status
command are older than hostname change logs, then it would show the old hostname. - Execute
hostname
command to confirm the correct hostname.
[root@node2 ~]# hostname
node2.example.com
- To have logs with new hostname, we need to either reload or restart the particular service, so that newly generated logs will have timestamp later than hostname change and would have the correct hostname reflected.
[root@node2 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-12-26 19:43:42 IST; 36s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 930 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 952 (sshd)
CGroup: /system.slice/sshd.service
└─952 /usr/sbin/sshd
Dec 26 19:43:42 node1.example.com systemd[1]: Starting OpenSSH server daemon...
Dec 26 19:43:42 node1.example.com systemd[1]: PID file /var/run/sshd.pid not readable (yet?) after start.
Dec 26 19:43:42 node1.example.com sshd[952]: Server listening on 0.0.0.0 port 22.
Dec 26 19:43:42 node1.example.com sshd[952]: Server listening on :: port 22.
Dec 26 19:43:42 node1.example.com systemd[1]: Started OpenSSH server daemon.
Dec 26 19:43:56 node1.example.com sshd[2155]: Accepted password for root from 10.10.1.1 port 56683 ssh2 <-- Shows old hostname
[root@node2 ~]# systemctl reload sshd <<--- Reload the sshd service
[root@node2 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2017-12-26 19:44:40 IST; 1s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2220 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Process: 930 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 952 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/sshd.service
└─2222 /usr/sbin/sshd
Dec 26 19:44:40 node2.example.com sshd[952]: Received SIGHUP; restarting.
Dec 26 19:44:40 node2.example.com systemd[1]: Reloaded OpenSSH server daemon.
Dec 26 19:44:40 node2.example.com sshd[2222]: Server listening on 0.0.0.0 port 22.
Dec 26 19:44:40 node2.example.com sshd[2222]: Server listening on :: port 22. <<-- Now shows correct hostname
根源
- The service was neither restarted nor reloaded after the hostname change, so
systemctl status
command would show logs with old hostname as they were recorded before the hostname change. - Timestamp of these logs recorded with old hostname would be older than the actual time of hostname change.
[root@node1 ~]# hostnamectl set-hostname node2.example.com
[root@node1 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-12-26 19:43:42 IST; 36s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 930 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 952 (sshd)
CGroup: /system.slice/sshd.service
└─952 /usr/sbin/sshd
Dec 25 19:43:42 node1.example.com systemd[1]: Starting OpenSSH server daemon...
Dec 25 19:43:42 node1.example.com systemd[1]: PID file /var/run/sshd.pid not readable (yet?) after start.
Dec 25 19:43:42 node1.example.com sshd[952]: Server listening on 0.0.0.0 port 22.
Dec 25 19:43:42 node1.example.com sshd[952]: Server listening on :: port 22.
Dec 25 19:43:42 node1.example.com systemd[1]: Started OpenSSH server daemon.
Dec 25 19:43:56 node1.example.com sshd[2155]: Accepted password for root from 10.10.1.1 port 56683 ssh2 <-- These logs were recorded before the hostname change
诊断步骤
- First, check for logs for hostname change, which would show the timestamp when the server hostname was changed.
# grep hostname /var/log/messages
Dec 26 19:43:34 node1.example.com systemd: Set hostname to <node2.example.com>.
Dec 26 19:44:17 node1.example.com dbus-daemon: dbus[620]: [system] Successfully activated service 'org.freedesktop.hostname1'
Dec 26 19:44:17 node1.example.com systemd-hostnamed: Changed static host name to 'node2.example.com'
Dec 26 19:44:17 node1.example.com NetworkManager[646]: <info> [1514297657.1859] settings: hostname changed from "node1.example.com" to "node2.example.com"
Dec 26 19:44:17 node1.example.com systemd-hostnamed: Changed host name to 'node2.example.com'
- Secondly, check timestamp for logs shown in systemctl status of any service and then compare with hostname change timestamp above.
# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-12-26 19:43:42 IST; 19s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 930 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 952 (sshd)
CGroup: /system.slice/sshd.service
└─952 /usr/sbin/sshd
Dec 25 19:43:42 desktop.example.com systemd[1]: Starting OpenSSH server daemon...
Dec 25 19:43:42 desktop.example.com systemd[1]: PID file /var/run/sshd.pid not readable (yet?) after start.
Dec 25 19:43:42 desktop.example.com sshd[952]: Server listening on 0.0.0.0 port 22.
Dec 25 19:43:42 desktop.example.com sshd[952]: Server listening on :: port 22.
Dec 25 19:43:42 desktop.example.com systemd[1]: Started OpenSSH server daemon.
Dec 25 19:43:56 desktop.example.com sshd[2155]: Accepted password for root from 10.10.1.1 port 56683 ssh2
- As we can see the difference in these timestamps, it means that sshd related logs were generated on Dec 25th before the hostname was changed on Dec 26th.