【问题标题】:Raspberry hostname change but not really覆盆子主机名更改但不是真的
【发布时间】:2018-10-15 21:32:35
【问题描述】:

我在 /etc/hosts 和 /etc/hostname 中更改主机名

主持人:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.0.1       newhost

主机名:

newhost

是的,它有效

>>> socket.gethostbyname('newhost')
'10.0.0.45'

但是

import socket
>>> socket.gethostbyaddr('10.0.0.45')
('raspberrypi', [], ['10.0.0.45'])

前段时间我使用了不同的名称,它可以双向使用,然后我更改了它,它一直向我显示 raspberrypi。我相信一定有一些默认文件会触发这个。任何人? 提前谢谢大家

【问题讨论】:

  • 使用sudo raspi-config彻底改变主机名。
  • 没有帮助或相关。我知道如何更改主机名,但我不知道为什么 pi 仍然在网络上显示为“raspberrypi”,即使我可以使用 socket.gethostbyname('newhost') 找到 pi

标签: python networking raspberry-pi host hostname


【解决方案1】:

raspi-config 似乎没有很好的文档记录,但更改主机名的解决方案是

sudo raspi-config nonint do_hostname ${NEW_HOSTNAME}
sudo reboot # Must reboot to see change

这是因为 raspi-config 是一个 bash 脚本,并且您可以使用非交互模式。其中,截至目前,这是我们试图在下面触发的:

do_hostname() {
  if [ "$INTERACTIVE" = True ]; then
    whiptail --msgbox "\
Please note: RFCs mandate that a hostname's labels \
may contain only the ASCII letters 'a' through 'z' (case-insensitive), 
the digits '0' through '9', and the hyphen.
Hostname labels cannot begin or end with a hyphen. 
No other symbols, punctuation characters, or blank spaces are permitted.\
" 20 70 1
  fi
  CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
  if [ "$INTERACTIVE" = True ]; then
    NEW_HOSTNAME=$(whiptail --inputbox "Please enter a hostname" 20 60 "$CURRENT_HOSTNAME" 3>&1 1>&2 2>&3)
  else
    NEW_HOSTNAME=$1
    true
  fi
  if [ $? -eq 0 ]; then
    echo $NEW_HOSTNAME > /etc/hostname
    sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
    ASK_TO_REBOOT=1
  fi
}

【讨论】:

    【解决方案2】:

    如果我说你只是想更改 pi 的主机名是正确的,并且你正在运行 raspbian,那么 raspberry pi 配置中应该有一个选项来更改它

    【讨论】:

    • 1.我不想这样做。 2. 新主机名已经在 raspi-config 中,所以没有帮助
    • 我已经更改了主机名,但在网络上它仍然显示为“raspberrypi”,但即使使用新主机名我也可以找到它。我只想要一个主机名。我希望 'raspberrypi' 作为主机名完全消失。 -> 这就是问题所在。为什么 raspberrypi 主机名仍然保持更改
    • 您使用的是最新版本的 raspbian 吗?
    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多