Intro
需要用到ssh免密码登录,虽然每次免密码登录可以简化为user_name@ip_address,但是还是嫌麻烦,希望可以实现 ssh user_name@host_name
的方式。
这里记录下ubuntu14.04LTS 修改主机名的方法
查看主机名
-
打开终端,在命令提示符中就可以看到主机名,比如我的就是
caffee@spotty
,那么我的用户名就是caffee
主机名就是spotty
; -
在终端中输入
uname -n
或者hostname
就可以看到当前主机的主机名
临时修改主机名
终端中运行: sudo hostname new_host_name
可以把当前主机名改为new_host_name,但是需要注意的是,new_host_name必须可以被resolve,也就是必须在 /etc/hosts
中按照key-value的形式注册过.比如这里我们注册一个abc的host
# /etc/hosts 127.0.0.1 localhost 127.0.1.1 spotty 192.168.71.137 mininet 127.0.0.1 abc # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
执行 sudo hostname abc
,这时候重新打开一个终端,就会出现临时主机名abc。
-
永久修改主机名
ubuntu的主机名存放在
/etc/hostname
中,直接sudo vim /etc/hostname
,修改完毕保存后,重启电脑,使用上面的方法即可判断主机名是否修改成功.
最后提一下 /etc/hostname
与 /etc/hosts
, /etct/hostname
就存放在了当前主机名, /etc/hosts
类似于windows下面的hosts文件,按照key-value的形式存放。
/etc/hostname
的文件内容一般如下:
controller
/etc/hosts
的文件内容一般如下:
27.0.0.1 localhost 127.0.1.1 spotty 192.168.71.137 mininet 127.0.0.1 abc # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters