kvm虚拟机由dhcp获取ip配置成静态ip

  • kvm虚拟机由dhcp获取ip配置成静态ip已关闭评论
  • 549 views
  • A+
所属分类:未分类

之前公司的云主机都是通过dhcp获取,通过NetworkManager管理的,现安装一些高可用服务的话就操蛋了,由于NetworkManager的原因,网络设备监听不到,没法弄了

症状如下:

ifconfig查看:

[root@cache-03 /etc/sysconfig/network-scripts]# ifconfig
eth1 Link encap:Ethernet HWaddr FA:16:3E:B2:22:2F 
 inet addr:10.209.33.129 Bcast:10.209.33.255 Mask:255.255.255.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:59513 errors:0 dropped:0 overruns:0 frame:0
 TX packets:57308 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000 
 RX bytes:4881646 (4.6 MiB) TX bytes:6687914 (6.3 MiB)

lo Link encap:Local Loopback 
 inet addr:127.0.0.1 Mask:255.0.0.0
 UP LOOPBACK RUNNING MTU:65536 Metric:1
 RX packets:398 errors:0 dropped:0 overruns:0 frame:0
 TX packets:398 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0 
 RX bytes:58676 (57.3 KiB) TX bytes:58676 (57.3 KiB)

看上去网卡名是eth1,但是用ifdown eth1,就报找不到设备名,如下:

[root@cache-03 /etc/sysconfig/network-scripts]# ifdown eth1
用法:ifdown <设备名>

如果这样的话,haproxy或者heartbeat就也起不了热备或者高可用作用了,然后我们看看网卡配置/etc/sysconfig/network-scripts下压根就没有ifcfg-eth1的相关信息,查看配置是NetworkManager在作怪。由于线上的云主机,自己也没有管理端,所有操作还得留些后路,所有我是先给/etc/sysconfig/network-scripts手动添加一块网卡信息,如下:

[root@cache-03 /etc/sysconfig/network-scripts]# more ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
HWADDR=FA:16:3E:B2:22:2F
IPADDR=10.9.3.19
PREFIX=24
GATEWAY=10.9.3.1

上面的信息,其他的我就不多说了,就说一下HWADDR是怎么来的吧,HWADDR是通过查询

[root@cache-03 /etc/sysconfig/network-scripts]# more /etc/udev/rules.d/70-persistent-net.rules 
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1af4:0x1000 (virtio-pci)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="fa:16:3e:b2:22:2
f", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

如上,是通过查看/etc/udev/rules.d/70-persistent-net.rules内容得来的,这里有必要说一下,NAME="eth1"必须跟ifcfg-eth1配置文件中的DEVICE一致

配置完成后我们下来要做的就是关闭NetworkManager,重启网卡,命令如下:

service NetworkManager stop;service network restart

ok了,一切搞定

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar