2021-08-28

CentOS7配置静态ip

问题

搭建服务器时, 发现网络连接方式是DHCP, 会自己分配ip地址, 导致刚搭好的服务器因为ip地址变化而不能连上.

遂决定找到方法固定服务器ip.

解决

百度之后发现很简单. 只要简单修改配置文件即可.

  1. 进入/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]$ 
  1. 修改网卡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
  1. 编辑完成后, 保存退出, 重新启动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/这篇更详细.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值