CentOS7配置静态ip
问题
搭建服务器时, 发现网络连接方式是DHCP, 会自己分配ip地址, 导致刚搭好的服务器因为ip地址变化而不能连上.
遂决定找到方法固定服务器ip.
解决
百度之后发现很简单. 只要简单修改配置文件即可.
- 进入/etc/sysconfig/network-scripts, 在目录中找到 “ifcfg-” 开头的配置文件.
[vito@localhost network-scripts]$ pwd
/etc/sysconfig/network-scripts
[vito@localhost network-scripts]$ ls -l ifcfg*
-rw-r--r--. 1 root root 442 Aug 27 23:00 ifcfg-ens33
-rw-r--r--. 1 root root 254 May 22 2020 ifcfg-lo
[vito@localhost network-scripts]$
- 修改网卡ens33的配置文件. 将文件中BOOTPROTO=“dhcp” 修改为"static".并添加相应的ip地址, 网关等
[vito@localhost network-scripts]$ vim ifcfg-ens33
BOOTPROTO="static"
DNS1=114.114.114.114
IPADDR=192.168.149.132
GATEWAY=192.168.149.2
PREFIX=24
NETMASK=255.255.255.0
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
- 编辑完成后, 保存退出, 重新启动network, 完成.
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.149.132 netmask 255.255.255.0 broadcast 192.168.149.255
inet6 fe80::1ed4:52f4:b3c1:db47 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b2:8c:3d txqueuelen 1000 (Ethernet)
RX packets 928579 bytes 1348145645 (1.2 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 257964 bytes 24418587 (23.2 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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 277532 bytes 1553615106 (1.4 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 277532 bytes 1553615106 (1.4 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost network-scripts]#
https://www.tecmint.com/set-add-static-ip-address-in-linux/这篇更详细.