CentOS 7 配置静态 ip

时间:2021-07-15 04:54:33

1. 需求场景:

在 CentOS7 上部署了一个 svn ,由于centOS7 默认是 dhcp 的,所以每过一段时间 ip 就会有变化,而 svn 要求 ip 是不能变的,不然没法同步。

网上找了篇 blog 参考: http://www.cnblogs.com/Leo_wl/p/5484136.html

2. 将/etc/sysconfig/network-scripts/ 下的网络连接文件配置为:

TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="440bdf0c-7e86-4586-b6b9-902a2e7b6cda"
DEVICE="eno16777736"
ONBOOT="yes"
IPADDR="192.168.178.133"  #静态 ip
GATEWAY="192.168.1.1"
NETMASK="255.255.255.0"
DNS1="192.168.1.1"  # 家里的电信路由器 ip
#HWADDR="00-50-56-C0-00-01"

3. service network restart 执行网络服务重启命令,但是显示失败:

Restarting network (via systemctl): Job for network.service failed. See *systemctl status network.service* and *journalctl -xn* for details.

根据以上提示查找问题,但没有发现有价值信息

4. 百度该问题,http://tieba.baidu.com/p/3233996339 得到建议

建议一. 修改 vmware 网络连接方式,由 NET 模式(共享主机模式)改为桥接模式,

改了之后执行 service network restart 启动真的成功了.
但是,地址还是自动分配的

建议二. 复制网络连接文件, ifcfg-eno16777736 到新文件 ifcfg-eth0

root@vm-xiluhua /etc/sysconfig/network-scripts # cp ifcfg-eno16777736 ifcfg-eth0
root@vm-xiluhua /etc/sysconfig/network-scripts # service network restart
Restarting network (via systemctl): [ 确定 ]

5. 再次 ip addr 查看,看到以太网卡 eno16777736 下面有两个 ip了,134 和 133

root@vm-xiluhua /etc/sysconfig/network-scripts # ip addr
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::a4:5f:1e brd ff:ff:ff:ff:ff:ff
inet 192.168.178.134/ brd 192.168.178.255 scope global dynamic eno16777736
valid_lft 1798sec preferred_lft 1798sec
inet 192.168.178.133/ brd 192.168.178.255 scope global secondary eno16777736
valid_lft forever preferred_lft forever
: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu qdisc noqueue state DOWN
link/ether :::c4:9c:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/ brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
: virbr0-nic: <BROADCAST,MULTICAST> mtu qdisc pfifo_fast master virbr0 state DOWN qlen
link/ether :::c4:9c:8b brd ff:ff:ff:ff:ff:ff

6. ping www.baidu.com 成功

root@vm-xiluhua /etc/sysconfig/network-scripts # ping www.baidu.com
PING www.a.shifen.com (115.239.211.112) () bytes of data.
bytes from www.baidu.com (115.239.211.112): icmp_seq= ttl= time=8.04 ms
bytes from 115.239.211.112: icmp_seq= ttl= time=7.99 ms
bytes from 115.239.211.112: icmp_seq= ttl= time=8.50 ms
bytes from 115.239.211.112: icmp_seq= ttl= time=8.00 ms
bytes from 115.239.211.112: icmp_seq= ttl= time=7.89 ms

7. windows

C:\Users\Administrator>ping 192.168.178.133

正在 Ping 192.168.178.133 具有  字节的数据:
来自 192.168.178.133 的回复: 字节= 时间<1ms TTL=
来自 192.168.178.133 的回复: 字节= 时间<1ms TTL=
来自 192.168.178.133 的回复: 字节= 时间<1ms TTL=

配置成功。

关键一步是第4步的,建议二。