Python 树莓的主机名改变了,但不是真的
我在/etc/hosts和/etc/hostname中更改主机名 主持人:Python 树莓的主机名改变了,但不是真的,python,networking,raspberry-pi,host,hostname,Python,Networking,Raspberry Pi,Host,Hostname,我在/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.geth
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'])
前一段时间,我用了不同的名字,它双向工作,然后我改变了它,它一直显示我树莓。我相信一定有一些默认文件触发了这一点。有人吗提前感谢各位如果我说您只想更改pi的主机名是正确的,并且您正在运行raspbian,那么raspberry pi配置中应该有一个选项来更改它raspi配置似乎没有很好的文档记录,但是更改主机名的解决方案是
sudo raspi-config nonint do_hostname ${NEW_HOSTNAME}
sudo reboot # Must reboot to see change
这是因为raspi配置是一个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
}
使用
sudo raspi config
彻底更改主机名。没有帮助或不相关。我知道如何更改主机名,但我不知道为什么pi在网络上仍然显示为“raspberrypi”,即使我可以通过socket.gethostbyname('newhost')1找到pi。我不想这样做。2.新主机名已经在raspi配置中,因此它没有帮助我已经更改了主机名,但在网络上它仍然显示为“raspberrypi”,但即使使用新主机名,我也可以找到它。我只想要一个主机名。我希望“raspberrypi”作为主机名被完全加密。->这就是问题所在。为什么raspberrypi主机名仍然保留更改您使用的是最新版本的raspbian吗?