Linux网络配置

Ubuntu

IP配置文件样例

设置静态IP+DNS

$ cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.30.140
netmask 255.255.255.0
gateway 192.168.30.1

$ cat /etc/resolvconf/resolv.conf.d/tail
nameserver 114.114.114.114
nameserver 223.5.5.5

关闭NetworkManager,设置managed=false

$ cat NetworkManager.conf
[main]
plugins=ifupdown,keyfile,ofono
dns=dnsmasq

no-auto-default=00:0C:29:0D:63:D2,

[ifupdown]
managed=false

网桥配置

### 新增网桥
# cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto ens3
iface ens3 inet static
address 192.168.200.13
netmask 255.255.255.0
gateway 192.168.200.1

auto br-mgmt
iface br-mgmt inet manual
### 重启网络服务
# systemctl restart networking

VLAN配置

# modprobe 8021q
# cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto eth0.91
iface eth0.91 inet manual
vlan-raw-device eth0

auto br-ex
iface br-ex inet static
address 192.168.200.13
netmask 255.255.255.0
gateway 192.168.200.1
bridge_ports eth0

指定网卡名

### 关闭系统命名规则
# if grep -q 'net.ifnames=0 biosdevname=0' /etc/default/grub; then \
echo "nothing to do"; \
else \
sed -i -e 's/GRUB_CMDLINE_LINUX="[^"]*/& net.ifnames=0 biosdevname=0/g' /etc/default/grub; \
fi
# update-grub

### 为网卡生成自定义命名规则,其中ens4要修改为系统当前ip addr能看到的网卡名字(即系统命令的名字),eth12就是自己指定的名字可以随意
# touch /etc/udev/rules.d/70-persistent-net.rules
# if grep -q 'eth12' /etc/udev/rules.d/70-persistent-net.rules; then \
echo "nothing to do"; \
else \
ip addr show ens4 | grep link/ether | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"`awk '{print $2}'`\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth12\"" >> /etc/udev/rules.d/70-persistent-net.rules; \
fi

### 上面所有修改完成后重启虚拟机
# reboot

CentOS/Fedora

IP配置文件样例

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
HWADDR=00:0c:29:12:cc:db
IPADDR=192.168.30.11
NETMASK=255.255.255.0
GATEWAY=192.168.30.2
DNS1=114.114.114.114
DNS2=8.8.8.8

网桥配置

# yum install bridge-utils
### 新增网桥
# cat /etc/sysconfig/network-scripts/ifcfg-br-mgmt
TYPE=Bridge
BOOTPROTO=static
NAME=br-mgmt
DEVICE=br-mgmt
ONBOOT=yes
IPADDR=10.0.0.1
NETMASK=255.255.255.0
### 重启网络服务
# systemctl restart network

VLAN配置

参考文档

# cat ifcfg-eth0.20
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0.20
DEVICE=eth0.20
ONBOOT=yes
IPADDR=10.0.0.1
NETMASK=255.255.255.0
BRIDGE=br-vxlan
VLAN=yes

# echo modprobe 8021q >> /etc/rc.modules
# chmod +x /etc/rc.modules
# systemctl disable NetworkManager
### 重启网络服务
# systemctl restart network

指定网卡名

方式一

### 生成eth0配置文件
# mv ifcfg-XX ifcfg-eth0
### 将NAME修改为ethX模式,DEVICE和HWADDR不动
# vim ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
ONBOOT=yes
NAME=eth0
DEVICE=XX
HWADDR=
# systemctl restart networking

方式二

### 修改GRUB_CMDLINE_LINUX属性添加net.ifnames=0 biosdevname=0,编辑完重启后网卡的命名方式自动变成ethX模式
# vim /etc/sysconfig/grub
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb net.ifnames=0 biosdevname=0 quiet"
# grub2-mkconfig -o /boot/grub2/grub.cfg

### 生成eth0配置文件
# vim ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
ONBOOT=yes
NAME=eth0
DEVICE=eth0

# reboot
posted @ 2017-10-14 10:04  银魔术师  阅读(511)  评论(0编辑  收藏  举报