ubuntu如何修改terminal终端的主机名

时间:2021-07-09 13:31:10
有时候安装完Ubuntu系统后,打开命令终端,终端显示的主机名格式比较难看,例如 我最近买的国内某云的VPS。
    xxx@VM-1560-ubuntu$ 
xxx@VM-1560-ubuntu$
对于有洁癖的人来说,看到这么长还带横杆的系统名就烦,想进各种方法消除他,还好,linux上命令修改特别方便。
一、关于hostname命令 hostname命令在linux上可以查看以及设置当前系统的名字 1. 查看hostname
      xxx@VM-1560-ubuntu$  hostname
VM-1560-ubuntu
2. 设置hostname
        xxx@VM-1560-ubuntu$  hostname  newhostname
hostname: you must be root to change the host name
这是因为修改hostname会改变系统属性,需要root权限。
        xxx@VM-1560-ubuntu$ sudo  hostname  newhostname
即可。
二、关于/etc/hostname文件 这个文件存储了hostname的名称,系统每次启动时都会通过读取这个文件获得系统的名称。 三、关于/etc/hosts 网络主机名称,可以通过修改hosts文件来重定向域名指向的ip。以前可以通过这个方法查看外面的花花世界,好像这两年不行了,哈哈,你懂的。
      xxx@VM-1560-ubuntu$ sudo vim /etc/hosts
127.0.0.1 localhost VM-1560-ubuntu
127.0.0.1即本机的IP。 四、修改主机名称 1. hostname修改系统名称
      xxx@VM-1560-ubuntu$  hostname  newhostname
2.修改/etc/hostname文件
      xxx@VM-1560-ubuntu$ sudo vim /etc/hostname
将VM-1560-ubuntu改为newhostname 3. 修改/etc/hosts文件
      xxx@VM-1560-ubuntu$ sudo vim /etc/hosts
修改前 127.0.0.1 localhost VM-1560-ubuntu 修改后 127.0.0.1 localhost newhostname 4. 打开新的终端,终端窗口的系统名称已经修改好了
      xxx@newhostname$
该方法适用于ubuntu系统,比如ubuntu16.04,ubuntu 16.10。