centos7设置静态ip和增加hostname访问

1.修改前查一下动态的gateway

[root@centos-node1 sandwich]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.71.2     0.0.0.0         UG        0 0          0 ens33
172.16.71.0     0.0.0.0         255.255.255.0   U         0 0          0 ens33
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

2.修改dhcp自动获取为static并添加ip地址

vi /etc/sysconfig/network-scripts/ifcfg-ens33

修改前

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="a4e373cd-4220-46b2-96e1-2a62ad99fe62"
DEVICE="ens33"
ONBOOT="yes"

BOOTPROTO="dhcp"改为BOOTPROTO="static"
添加静态ip, gateway和dns
修改后

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
#BOOTPROTO="dhcp"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="a4e373cd-4220-46b2-96e1-2a62ad99fe62"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="172.16.71.101"
GATEWAY="172.16.71.2"
DNS2="172.16.71.2"

3.添加hostname(永久有效)

hostnamectl set-hostname centos-node1

4.绑定hostname 和 静态ip
在/etc/hosts下添加172.16.71.101 centos-master

[root@centos-node1 sandwich]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.71.101 centos-master

5.把想直接访问hostname的机器也加上ip和hostname的映射

sandwich@ZBook-G3:~$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       ZBook-G3

# 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
172.16.71.131 centos-master
172.16.71.101 centos-node1

6.测试访问

sandwich@ZBook-G3:~$ ssh centos-node1
sandwich@centos-node1's password: 
Last login: Sat Feb 27 19:29:32 2021 from 172.16.71.1
[sandwich@centos-node1 ~]$ 

你可能感兴趣的:(centos7设置静态ip和增加hostname访问)