在阿里云买的服务器ESC,新创建的实例是自动生成的一串字符,不方便平常使用,可以通过命令hostname进行修改。

通过hostname查看云服务器主机名

[root@test ~]# hostname
test
[root@test ~]# 

临时生效

通过hostname进行修改,输入命令hostname 主机名,重新登录 shell 生效。这个命令的作用是暂时修改linux的主机名,它的存活时间是linux当前的运行时间,也就是在下一次重启前的运行时间内。一般修改以后立即生效,但这只是临时生效,不是永久生效,重启系统之后会还原。

[root@centos7 ~]# hostname test
[root@centos7 ~]# 

重新登录shell之后主机名已经被修改

[root@test ~]# 

永久方法

通过修改配置文件/etc/sysconfig/network

在这个配置文件中底部添加一条HOSTNAME=主机名(可自定义),这个修改它能够实现永久修改linux的主机名,但是它不会立即生效,也不会重新登录shell生效,而是要重启系统才能生效。通过修改此配置文件,再配合hostname命令,可以实现立即永久修改linux的主机名,当然了,至少需要重启shell才可以。

[root@test ~]# cat /etc/sysconfig/network
# Created by anaconda
NETWORKING_IPV6=no
PEERNTP=no
[root@test ~]# 

如果是 Ubuntu 系统,则需要修改文件 /etc/hostname, 将其对应的主机名修改为新的主机名。
最后,需要将 /etc/hosts 中 127.0.0.1 对应的老主机名更换为新的主机名(如果有需要)。

[root@test ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# 
通过命令hostnamectl set-hostname 主机名修改

centos7系统可以直接通过命令hostnamectl set-hostname 主机名进行修改,修改完毕后重新登录 shell 就可以了。

[root@test ~]# hostnamectl set-hostname centos7
[root@test ~]# 
GitHub 加速计划 / li / linux-dash
12
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐