网络配置

centos6

主机名

/etc/sysconfig/network

hostname redhat:当前生效

vim /etc/sysconfig/network:重启后生效

网卡名

/etc/udev/rules.d/70-persistent-net.rules

修改网卡名

  1. 修改网卡的名字

    [root@centos6 network-scripts]# vim /etc/udev/rules.d/70-persistent-net.rules
    
  2. 修改网卡配置文件的名字

    [root@centos6 network-scripts]# mv ifcfg-eth3 ifcfg-eth2
    
  3. 修改配置文件中的网卡名字

    [root@centos6 network-scripts]# vim ifcfg-eth3
    
  4. 查看网卡模块

    [root@centos6 network-scripts]# ethtool -i eth2
    
  5. 重启网卡模块

    [root@centos6 network-scripts]# modprobe -r e1000
    [root@centos6 network-scripts]# modprobe e1000
    

配置bonding

创建bonding

  1. 增加网卡并配置bond0文件

    [root@centos6 ~]# vim /etc/sysconfig/network-scripts/ifcfg-bond0
    DEVICE=bond0
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    BONDING_OPTS="miimon=100  mode=1"
    IPADDR=1.1.1.1
    NETMASK=255.255.255.0
    
  2. 配置网卡文件

    [root@centos6 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth3
    DEVICE=eth3
    HWADDR=00:50:56:3d:28:04
    TYPE=Ethernet
    ONBOOT=yes
    MASTER=bond0
    SLAVE=yes
    
    [root@centos6 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth4
    EVICE=eth4
    HWADDR=00:50:56:37:2b:03
    TYPE=Ethernet
    ONBOOT=yes
    MASTER=bond0
    SLAVE=yes
    
  3. 重启网络服务

    [root@centos6 ~]# service network restart
    
  4. 查看bond状态

    [root@centos6 ~]# cat /proc/net/bonding/bond0
    [root@centos6 ~]# cat /sys/class/net/bond0/bonding/mode
    

删除bonding

  1. 禁用bond0

    [root@centos6 ~]# ifconfig bond0 down
    
  2. 卸载bonding模块

    [root@centos6 ~]# modprobe -r bonding
    [root@centos6 ~]# rmmod bonding
    
  3. 删除bond0及相关网卡的配置文件

    [root@centos6 ~]# rm /etc/sysconfig/network-scripts/ifcfg-bond0
    [root@centos6 ~]# rm /etc/sysconfig/network-scripts/ifcfg-eth{3,4}
    
  4. 可以选择再将模块加载

    [root@centos6 ~]# modprobe bonding
    

centos7

主机名

/etc/hostname

默认无文件,通过DNS反向解析获取主机名,主机名默认为:localhost.localdomain

hostnamectl set-hostname redhat:修改主机名,立即生效并写入配置文件

网卡名

修改网卡命名方式为传统命名方式,为方便自动化运维,通常会修改

修改网卡名

  1. 先修改命名方式,两种修改方式原理相同

    # 方法一
    [root@centos7 ~]# vim /boot/grub2/grub.cfg
    在linux16...行的尾加上net.ifnames=0
    
    # 方法二
    [root@centos7 ~]# vim /etc/default/grub
    在rhgb quiet后面加上 net.ifnames=0
    [root@centos7 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
    重新生成带net.ifnames=0配置的新grub.cfg文件
    
  2. 修改配置文件,先改配置文件名称,再改配置文件中的设备名称

    [root@centos7 ~]# mv ifcfg-ens33 ifcfg-eth0
    [root@centos7 ~]# vim ifcfg-eth0
    修改DEVICE
    
  3. 重启系统

nmcli

基本配置

禁用指定网络接口:nmcli device disconnect eth0
启用指定网络接口:nmcli device connect eth0

显示网络接口属性:nmcli decice show eth0
显示设备状态:nmcli device status

创建新连接:nmcli connection add type ethernet con-name eth2-redhat ifname eth2(/etc/sysconfig/network-scripts/下的配置文件名称为初次创建时指定的名称,以后修改只是修改配置文件中的名称,而不会修改配置文件本身的名称)

修改连接名称:nmcli connection modify eth2-redhat con-name eth2-redhat
修改连接配置:nmcli connection modify eth0-office ipv4.method manual ipv4.addresses 1.1.1.1/24 ipv4.gateway 1.1.1.254 ipv4.dns 8.8.8.8

切换连接配置:nmcli connection up eth0-office

显示所有连接(包括不活动的):nmcli connection show
显示所有活动连接:nmcli connection show --active
显示网络连接配置:nmcli connection show ens33-hi

增加ipv4.address:nmcli connection modify eth0-office +ipv4.addresses 3.3.3.3/24
减少ipv4.address:nmcli connection modify eth0-office -ipv4.addresses 3.3.3.3/24

重读配置:nmcli connection reload(nmcli直接修改配置,up一下即可生效,如果直接修改配置文件,要使用reload将硬盘中的配置文件读入内存,再up重读配置才能生效)

DHCP时不自动获取DNS:nmcli connection modify eth0-office ipv4.ignore-auto-dns yes(当IP通过dhcp自动获取时,dns仍是手动设置,不自动获取,相当于读取配置文件中DNS地址)

删除配置文件:nmcli connection delete ens33

配置bonding

创建bonding

  1. 增加bond配置文件

    [root@centos7 ~]# nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup
    
  2. 将物理网卡作为slave加入bond

    [root@centos7 ~]# nmcli connection add type bond-slave ifname eth2 con-name bond-slave-eth2 master bond0
    [root@centos7 ~]# nmcli connection add type bond-slave ifname eth3 con-name bond-slave-eth3 master bond0
    
  3. 给bond0设置ip

    [root@centos7 ~]# nmcli connection modify bond0 ipv4.method manual ipv4.addresses 1.1.1.1/24
    
  4. 启用slave

    [root@centos7 ~]# nmcli connection up bond-slave-eth2
    [root@centos7 ~]# nmcli connection up bond-slave-eth3
    
  5. 启用bonding

    [root@centos7 ~]# nmcli connection up bond0
    

配置team

创建team

  1. 增加team配置文件

    [root@centos7 ~]# nmcli connection add type team con-name team0 ifname team0 config '{"runner": {"name": "loadbalance"}}'
    
  2. 将物理网卡作为slave加入team

    [root@centos7 ~]# nmcli connection add type team-slave con-name team0-slave-eth2 ifname eth2 master team0
    [root@centos7 ~]# nmcli connection add type team-slave con-name team0-slave-eth3 ifname eth3 master team0
    
  3. 给team0设置ip

    [root@centos7 ~]# nmcli connection modify team0 ipv4.method manual ipv4.addresses 1.1.1.1/24
    
  4. 启用slave

    [root@centos7 ~]# nmcli connection up bond-slave-eth2
    [root@centos7 ~]# nmcli connection up bond-slave-eth3
    
  5. 启用team

  6. 
    

    修改或查看team

    [root@centos7 ~]# nmcli connection modify team0 team.config '{"runner": {"name": "activebackup"}}'
    [root@centos7 ~]# teamdctl team0 state
    

配置bridge

创建bridge

  1. 增加bridge配置文件

    [root@centos7 ~]# nmcli connection add type bridge con-name br0 ifname br0
    
  2. 将物理网卡作为slave加入bridge

    [root@centos7 ~]# nmcli connection add type bridge-slave con-name br0-eth4 ifname eth4 master br0
    
  3. 修改bridge配置

    [root@centos7 ~]# nmcli connection modify br0 ipv4.method manual ipv4.addresses 2.2.2.2/24
    
  4. 启用查看bdidge

    [root@centos7 ~]# nmcli connection up br0
    [root@centos7 ~]# brctl show
    
  5. 删除bridge中的网卡

    [root@centos7 ~]# brctl delif br0 eth4
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容