linux网络配置
一、linux网络配置方式
1. sysconfig
路径:/etc/sysconfig/network-script/
这里面有很多的各种子接口的配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-br0 ifdown-eth ifdown-routes ifup-bnep ifup-plusb ifup-tunnel
ifcfg-br1 ifdown-ib ifdown-sit ifup-eth ifup-post ifup-wireless
ifcfg-enp1s0 ifdown-ippp ifdown-Team ifup-ib ifup-ppp init.ipv6-global
ifcfg-enp2s0 ifdown-ipv6 ifdown-TeamPort ifup-ippp ifup-routes network-functions
ifcfg-lo ifdown-isdn ifdown-tunnel ifup-ipv6 ifup-sit network-functions-ipv6
ifdown ifdown-post ifup ifup-isdn ifup-Team
ifdown-bnep ifdown-ppp ifup-aliases ifup-plip ifup-TeamPort
需要注意的是编辑完成后需要执行 service network restart才能生效;
2./etc/rc.local启动脚本配置
这种方式系统起来后通过运行脚本对网络进行配置;
NOTE:第二种脚本方式用起来很方便,但是有个硬伤,service network restart对应rc.local里面的网络配置点是 不会进行执行的,因此我们不推荐在rc.local里面运行
[root@localhost ~]# vi /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
dhclient br1
~
二、详细配置说明:
sysconfig网络配置主要点
[root@localhost network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
IPADDR=192.168.10.1
PREFIX=16
DNS1=192.168.10.1
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
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
DEVICE=br0
ONBOOT=no
[root@localhost network-scripts]# cat ifcfg-enp1s0
TYPE=Ethernet
BOOTPROTO=none /*none static dhcp是那种方式*/
NAME=enp1s0
UUID=df903812-a7cf-4680-ab0d-a00a094cb54c
DEVICE=enp1s0
ONBOOT=yes
BRIDGE=br0 /* 将enp1s0加入br0桥接口 */
NM_CONTROLLED=yes /* 该字段的含义主要是标明是否被network manager控制,yes为控制则启动后会被配置运行,曾经遇到dhcp无法获取的问题,因为没有配置该配置导致 */
命令方式配置
IP地址配置和查看
- 接口配置情况查看
ifconfig
- 静态配置ip地址
ipconfig enp1s0 192.168.10.1 netmask 255.255.0.0 /* 静态配置enp1s0的ip地址 */
- 动态获取ip地址
dhclient br1 /* br1口动态获取ip地址 */
vlan配置
ip link add link enp1s0 name enp1s0.6 type vlan id 6
通过创建子接口的方式,放行对应的配置vlanid的接口,但是从实际验证看linux自身支持带tag的报文通过对应接口
路由配置
- 路由查看配置
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.30.102.1 0.0.0.0 UG 426 0 0 br1
10.10.0.1 0.0.0.0 255.255.255.255 UH 425 0 0 br0
172.30.102.0 0.0.0.0 255.255.255.0 U 425 0 0 br1
172.30.103.0 172.30.102.1 255.255.255.0 UG 0 0 0 br1
172.30.255.50 172.30.102.1 255.255.255.255 UGH 425 0 0 br1
192.168.0.0 0.0.0.0 255.255.0.0 U 425 0 0 br0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
- 添加路由
route add -net 172.30.103.0 netmask 255.255.255.0 gw 172.30.102.1
ARP查看
arp -a