Este tópico explica como configurar um endereço IP estático em uma instância ECS Linux.
Contexto
Por padrão, as instâncias ECS usam o modo Dynamic Host Configuration Protocol (DHCP).
Este exemplo usa uma instância ECS com CentOS 7.6.
Procedimento
Etapa 1: Visualize o endereço IP e o gateway
-
Faça login na instância ECS Linux e execute o comando abaixo para visualizar o endereço IP e a máscara de sub-rede.
ifconfigA saída será semelhante à seguinte:
[root@aliyuntest ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 17.xxx.xxx.xx netmask 255.255.255.0 broadcast 172.xxx.xxx.x ether 00:16:3e:0c:4f:c3 txqueuelen 1000 (Ethernet) RX packets 285116 bytes 217057111 (207.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 143703 bytes 40663281 (38.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 509 bytes 102908 (100.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 509 bytes 102908 (100.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 -
Execute o comando a seguir para verificar o gateway da instância.
route -nA saída será semelhante à seguinte:
[root@alxxx : ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.xxx.xxx.53 0.0.0.0 UG 0 0 0 eth0 169.xxx.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 172.xxx.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
Alternativamente, use os comandos abaixo para obter a máscara de sub-rede, o gateway e o endereço IP nos metadados da instância. Para mais informações, consulte Metadados da instância.
curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/netmask
curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/gateway
curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/private-ipv4s
A saída será semelhante à seguinte:
[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/netmask
255.255.255.0[root@aliyuntest ~]#
[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/gateway
172.xxx.xxx[root@aliyuntest ~]#
[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/private-ipv4s
["172.xxx.xxx.0"][root@aliyuntest ~]#
[root@aliyuntest ~]#
Substitua [$MAC_ID] pelo endereço MAC da sua instância ECS.
Etapa 2: Alterar o tipo de rede para estático
-
Edite o arquivo de configuração com o comando a seguir. Defina BOOTPROTO como static e adicione o IP address, o gateway e a subnet mask anotados na etapa anterior.
vim /etc/sysconfig/network-scripts/ifcfg-eth0Exemplo:
DEVICE=eth0 BOOTPROTO=static ONBOOT=yes IPADDR=172.xxx.xxx GATEWAY=17x.xxx.253 NETMASK=255.255.255.0 Insira
:wqe pressione Enter para salve e sair.-
Reinicie o serviço de rede com o comando abaixo.
systemctl restart network