Why systemctl status command for any service shows different hostname than actual hostname ?

博客围绕Red Hat Enterprise Linux 7系统中,主机名更改后服务日志仍显示旧主机名的问题展开。分析根源是主机名更改后服务未重启或重载,日志时间戳早于主机名更改时间。给出的决议是检查日志时间戳,重启或重载服务。还介绍了诊断步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://access.redhat.com/solutions/2985791

 

 SOLUTION 已验证 

已验证此解决方案可由红帽客户和支持工程师用于指定的产品版本。

- 已更新 2018年十月10日03:27 - 

English 

环境

  • 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 to node2.example.com but systemctl status command for few services still shows hostname as node1.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.

Raw

[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.

Raw

[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.

Raw

[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.

Raw

 # 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.

Raw

# 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值