python - 覆盆子主机名更改但不是真的
问题描述
我在 /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。我相信一定有一些默认文件会触发这个。任何人?提前谢谢各位
解决方案
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
}