raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

时间:2024-03-13 09:30:09
raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

raspberry pi

raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

The default hostname for the Raspberry Pi is, creatively enough, “raspberrypi“. What if you want a different hostname or you want to avoid hostname conflicts on your local network? Read on as we show you how to quickly change the hostname of a Linux-based device.

创造性地,Raspberry Pi的默认主机名是“ raspberrypi ”。 如果您要使用其他主机名,或者要避免在本地网络上发生主机名冲突,该怎么办? 继续阅读,我们将向您展示如何快速更改基于Linux的设备的主机名。

我为什么要这样做? (Why Do I Want to Do This?)

There are two primary reasons why you would want to take a few minutes to edit the local hostname of a Linux device on your network. The most common reason would simply be customization–it’s fun to personalize things. Rather than leave your Raspberry Pi music station as plain old “raspberrypi“, for example, you could rename it to “jukebox“.

为什么要花几分钟时间来编辑网络上Linux设备的本地主机名,主要有两个原因。 最常见的原因只是定制-个性化设置很有趣。 而不是留下您的树莓派音乐台为纯老“ raspberrypi “,例如,您可以将其重命名为“ jukebox “。

The other reason you would want to customize the local host is to avoid name conflicts. If you, for example, have purchased and set up three Raspberry Pi units, all three of them (assuming a default Raspbian installation) will attempt to claim the local hostname “raspberrypi“.

您想要自定义本地主机的另一个原因是避免名称冲突。 例如,如果您购买并设置了三个Raspberry Pi单元,则所有三个(假设是默认的Raspbian安装)将尝试声明本地主机名“ raspberrypi ”。

raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

The first one will succeed and the next two will fail to resolve their hostnames, leaving them blank in your router’s device list (as seen in the screenshot above) and unreachable via hostname-based protocols like Samba file sharing.

前一个将成功,而后两个将无法解析其主机名,从而使它们在路由器的设备列表中为空(如上图所示),并且无法通过基于主机名的协议(例如Samba文件共享)访问。

Fortunately it’s super simple, assuming you know where to perform a few quick edits, to change the hostname of your Raspberry Pi (and most other Linux-based devices you have full access to). For demonstration purposes we’ll be performing the change on a stock Raspbian installation, but the same file edits will work on Debian, Ubuntu, and most other Linux platforms.

幸运的是,它非常简单,假设您知道在哪里执行一些快速编辑,更改Raspberry Pi(以及您可以完全访问的大多数其他基于Linux的设备)的主机名。 出于演示目的,我们将在Raspbian原始安装上进行更改,但是相同的文件编辑将在Debian,Ubuntu和大多数其他Linux平台上起作用。

更改Pi上的主机 (Changing the Host on your Pi)

We have so many Raspberry Pi units around the office that a bunch of them are now in conflict. Today we’re going to fix that by assigning unique names to each Pi unit based on their current function. A perfect candidate for this renaming is our awesome Raspberry Pi weather station; it will be much easier to identify it on the network once we change the hostname to “weatherstation“.

我们办公室周围有太多的Raspberry Pi装置,以至于其中很多现在发生冲突。 今天,我们将通过根据每个Pi单元的当前功能为其分配唯一名称来解决此问题。 我们令人敬畏的Raspberry Pi气象站是进行这个重命名的最佳候选人; 一旦我们将主机名更改为“ weatherstation ”,在网络上识别它就会容易得多。

The first step is to either open up the terminal on the device or to SSH into the device and open up a remote terminal. Our device is headless and currently running, so we’ll take the remote terminal route and connect to it via SSH.

第一步是打开设备上的终端或通过SSH进入设备并打开远程终端。 我们的设备无头且当前正在运行,因此我们将采用远程终端路由并通过SSH连接到它。

At the terminal, type the following command to open the hosts file:

在终端上,键入以下命令以打开主机文件:

sudo nano /etc/hosts

sudo nano /etc/hosts

Your hosts file will look like so:

您的主机文件如下所示:

raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

Leave all of the entries alone except for the very last entry labeled 127.0.1.1 with the hostname “raspberrypi“. This is the only line you want to edit. Replace “raspberrypi” with whatever hostname you desire. We replaced it on our device with “weatherstation“. Press CTRL+X to close the editor; agree to overwrite the existing file and save it.

保留所有条目的单独除非常最后一个条目标记127.0.1.1与主机名“ raspberrypi “。 这是您要编辑的唯一一行。 将“ raspberrypi”替换为所需的任何主机名。 我们在设备上将其替换为“ weatherstation ”。 按CTRL + X关闭编辑器; 同意覆盖现有文件并保存。

Back at the terminal, type the following command to open the hostname file:

返回终端,键入以下命令以打开主机名文件:

sudo nano /etc/hostname

sudo nano /etc/hostname

This file only contains your current hostname:

该文件仅包含您当前的主机名:

raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

Replace the default “raspberrypi” with the same hostname you put in the previous step (e.g. “weatherstation“). Again, press CTRL+X to close the editor, agree to overwrite the existing file and save it.

将默认的“ raspberrypi ”替换为上一步中输入的主机名(例如“ weatherstation ”)。 再次,按CTRL + X关闭编辑器,同意覆盖现有文件并保存。

Finally, we need to commit the changes to the system and reboot the system for the changes to take effect. At the terminal, enter the following command to commit the changes:

最后,我们需要将更改提交到系统并重新启动系统,以使更改生效。 在终端上,输入以下命令以提交更改:

sudo /etc/init.d/hostname.sh

sudo /etc/init.d/hostname.sh

Follow that command with:

遵循以下命令:

sudo reboot

sudo reboot

Once the system comes back online, you can check the device list in your router to see if the new hostname has properly resolved:

系统重新联机后,您可以检查路由器中的设备列表,以查看新主机名是否已正确解析:

raspberry pi_如何更改您的Raspberry Pi(或其他Linux设备)的主机名

Success! Now instead of wandering the network without a name, our little Raspberry Pi weather station has a hostname all its own.

成功! 现在,我们的小树莓派(Raspberry Pi)气象站不再是一个没有名字的漫游网络,而是拥有自己的主机名。

翻译自: https://www.howtogeek.com/167195/how-to-change-your-raspberry-pi-or-other-linux-devices-hostname/

raspberry pi