Linux系统下如何修改主机名

Linux系统下如何修改主机名,第1张

工具/材料

一台装有linux系统的电脑

临时修改01

先打开linux系统,然后打开终端,按步骤输入即可

其中fishc@fish, fishc代码用户名,fish代表主机名

02

完成上面的步骤后,然重新开一个终端就会看到效果

重启系统后,会失效

永久主机名修改01

修改hosts文件

先切换到root用户,进入root用户后,输入vi /etc/hosts

使用上下左右键中的右键移动鼠标,修改主机名

02

修改后的效果如下图,

退出时先按一下esc键,然后按住shift不放,再按住两次z,退出并保存

03

修改network文件

vi /etc/sysconfig/network

04

退出时先按一下esc键,然后按住shift不放,再按住两次z,退出并保存

然后reboot,修改成功

1,通过修改文件/etc/sysconfig/network .  eg:

# cat /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=Server34 ---->修改想要的名字

   2,修改文件/etc/hosts

127.0.0.1   localhost Server34 localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

3,reboot 既可修改原主机名。

(二)CentOS7.2修改主机名

CentOS7.2修改主机名相对于6.5版本又有所改进,不需要重启就可以直接修改,具体的方法有如下三种:

(1)修改配置文件/etc/hostname来进行修改

12345

   

[root@localhost ~]# vim /etc/hostname server23[root@localhost ~]# systemctl restart systemd-hostnamed.service[root@localhost ~]# hostnameserver23

   

(2)通过nmtui修改主机名

12345678910111213

   

[root@localhost ~]# nmtui                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ┌─────────────────┤ Set Hostname ├──────────────────┐                                                                       │                                                   │                                                                       │ Hostname Server23________________________________ │                                                                       │                                                   │                                                                       │                                     <Cancel> <OK> │                                                                       │                                                   │                                                                       └───────────────────────────────────────────────────┘                                                                                                                                                                 [root@localhost ~]# hostnameServer23

   

 (3) hostnamectl的命令行工具

123456789101112131415161718192021222324252627282930313233

   

            [root@localhost ~]# hostnamectl -helphostnamectl [OPTIONS...] COMMAND ...Query or change system hostname.  -h --help              Show this help     --version           Show package version     --no-ask-password   Do not prompt for password  -H --host=[USER@]HOST  Operate on remote host  -M --machine=CONTAINER Operate on local container     --transient         Only set transient hostname     --static            Only set static hostname     --pretty            Only set pretty hostnameCommands:  status                 Show current hostname settings  set-hostname NAME      Set system hostname  set-icon-name NAME     Set icon name for host  set-chassis NAME       Set chassis type for host  set-deployment NAME    Set deployment environment for host  set-location NAME      Set location for host[root@localhost ~]# hostnamectl status   Static hostname: Server23         Icon name: computer-vm           Chassis: vm        Machine ID: b832bc29266640fe83d7256d53840cdc           Boot ID: 33590b06c20b45bab1e8e12fbf4986d2    Virtualization: microsoft  Operating System: CentOS Linux 7 (Core)       CPE OS Name: cpe:/o:centos:centos:7            Kernel: Linux 3.10.0-327.el7.x86_64      Architecture: x86-64[root@localhost ~]# hostnamectl set-hostname servers23[root@localhost ~]# hostnameservers23

   


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8689936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存