在 Linux 中将主机名添加到 /etc/hosts

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5144738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:03:51  来源:igfitidea点击:

Adding host name to /etc/hosts in Linux

linuxnetworkingubuntu

提问by M99

Question: Is there an easy way to add DHCP issued IP address and Hostname of a linux machine in /etc/hosts at System startup automatically?

问题:有没有一种简单的方法可以在系统启动时自动在 /etc/hosts 中添加 DHCP 发布的 IP 地址和 Linux 机器的主机名?

Background: My Linux machine has a hostname in /etc/hostname and it won't resolve to anything when I ping. I manually added my hostname and ip address in /etc/hosts for one my network related java programs to work.

背景:我的 Linux 机器在 /etc/hostname 中有一个主机名,当我 ping 时它不会解析为任何内容。我在 /etc/hosts 中手动添加了我的主机名和 IP 地址,以便我的网络相关 java 程序正常工作。

Thanks,

谢谢,

采纳答案by Marc B

dhcpcd has a -c/--scriptoption to run an external script anytime it configures or brings up an interface. You can use this to manually update the hosts file with the configured hostname.

dhcpcd 可以-c/--script选择在配置或调出界面时随时运行外部脚本。您可以使用它来手动更新带有配置的主机名的主机文件。

回答by Diego Torres Milano

Use avahi(which should be on your distro repositories), then you can

使用avahi(应该在您的发行版存储库中),然后您可以

$ ping youhostname.local

回答by Markus Pscheidt

In Ubuntu, add an executable file into the /etc/network/if-up.ddirectory. Files in this directory get executed after the network manager configures a network interface.

在 Ubuntu 中,将一个可执行文件添加到/etc/network/if-up.d目录中。此目录中的文件在网络管理员配置网络接口后执行。

You may adapt the following script :

您可以修改以下脚本:

#!/bin/sh

set -e

if [ "$IFACE" = lo ]; then
    exit 0
fi

myHostName=`hostname`

# Remove current line with hostname at the end of line ($ means end of line)
sed -i '/'$myHostName'$/ d' /etc/hosts

ipaddr=$(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print }')
echo "$ipaddr $myHostName" >>/etc/hosts

回答by nomadic_squirrel

I took what @Markus did and put it into a normal script. This works on my Fedora 20 box:

我拿了@Markus 所做的并将其放入普通脚本中。这适用于我的 Fedora 20 盒子:

#!/bin/sh

MYHOST=firtree

echo "before:"
cat /etc/hosts

# Remove current line with hostname at the end of line ($ means end of line)
sed -i '/'$MYHOST'$/ d' /etc/hosts

echo "after remove: "
cat /etc/hosts

IPADDR=$(ifconfig | awk -F" +|:" '/inet addr/ &&  != "127.0.0.1" {print }')
echo "$IPADDR $MYHOST" >>/etc/hosts

echo "ip: " $IPADDR
echo "final: "
cat /etc/hosts

This does have to be run as root, and probably should go in an init.d folder.

这确实必须以 root 身份运行,并且可能应该放在 init.d 文件夹中。

回答by deepujain

From

ipaddr=$(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print }')
host=`hostname`
fhost=`hostname -f`

echo "$ipaddr $fhost $host" >> /etc/hosts

cat /etc/hosts

回答by Tsioneb

I personally use this script to set my hostname (existing one) + dynamic IP to /etc/hostsfile :

我个人使用此脚本将我的主机名(现有的)+ 动态 IP 设置为/etc/hosts文件:

#!/bin/bash
ipaddr=$(/sbin/ifconfig eth0| grep 'inet addr' | cut -d: -f2 | awk '{print }')
hn=$(hostname)
hnd=$(hostname -f)
sed -i '2s/.*/'$ipaddr'  '$hnd'   '$hn'/' /etc/hosts

Kind regards,

亲切的问候,