关键字: Linux Ubuntu 主机名 hostname /etc/hostname /etc/hosts Unable to resolve hostname
一.需要修改两个地方
/etc/hostname
/etc/hosts
shily@hh-laptop:~$ cat /etc/hostname
hh-laptop
shily@hh-laptop:~$ cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
127.0.1.1 hh-laptop
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
shily@hh-laptop:~$
其中深色的地方需要统一, 否则会出现 Unable to resolve hostname的错误
二. 新建hostname的脚本[把下面内容保存文本中,并添加执行权限]
#!/bin/sh
host_name=`hostname`
host_name_new=
read -p "Input the new hostname[$host_name]: " host_name_new
echo "the hostname new you input is: $host_name_new"
if [ "$host_name_new" != "" ] && [ "$host_name_new" != "$host_name" ]
then
sed -i-`date '+%Y%m%d%H%M'` 's/'${host_name}'/'${host_name_new}'/g' /etc/hostname /etc/hosts
if [ $? -eq 0 ]
then
echo "hostname changed from [$host_name] to [$host_name_new]."
else
echo "execute error, are you root?"
fi
else
echo "hostname doesn't changed, leave [$host_name] default."
一.需要修改两个地方
/etc/hostname
/etc/hosts
shily@hh-laptop:~$ cat /etc/hostname
hh-laptop
shily@hh-laptop:~$ cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
127.0.1.1 hh-laptop
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
shily@hh-laptop:~$
其中深色的地方需要统一, 否则会出现 Unable to resolve hostname的错误
二. 新建hostname的脚本[把下面内容保存文本中,并添加执行权限]
#!/bin/sh
host_name=`hostname`
host_name_new=
read -p "Input the new hostname[$host_name]: " host_name_new
echo "the hostname new you input is: $host_name_new"
if [ "$host_name_new" != "" ] && [ "$host_name_new" != "$host_name" ]
then
sed -i-`date '+%Y%m%d%H%M'` 's/'${host_name}'/'${host_name_new}'/g' /etc/hostname /etc/hosts
if [ $? -eq 0 ]
then
echo "hostname changed from [$host_name] to [$host_name_new]."
else
echo "execute error, are you root?"
fi
else
echo "hostname doesn't changed, leave [$host_name] default."
fi
From:http://hi.baidu.com/ati_crossfire/blog/item/f5b5db2de6fe01e08a1399f2.html