七、网络管理

wangzhaoyang 发布于 15 天前 29 次阅读 网络工程中的Linux系统 无~ 1113 字


ifconfig

[root@zabbix ~]# yum -y install net-tools
$ ifconfig eth0 up                        # 关闭网卡
$ ifconfig eth0 down                    # 激活网卡
$ ifconfig   #处于激活状态的网络接口
$ ifconfig -a  #所有配置的网络接口,不论其是否激活
$ ifconfig eth0  #显示eth0的网卡信息
$ ifconfig eth0 add 33ffe:3240:800:1005::2/64    # 为网卡eth0配置IPv6地址
$ ifconfig eth0 del 33ffe:3240:800:1005::2/64    # 为网卡eth0删除IPv6地址
$ ifconfig eth0 hw ether 00:AA:BB:CC:dd:EE             # 修改MAC地址
$ ifconfig eth0 arp    #开启网卡eth0 的arp协议
$ ifconfig eth0 -arp   #关闭网卡eth0 的arp协议
$ ifconfig eth0 mtu 1500    #设置能通过的最大数据包大小为 1500 bytes



# 配置IP地址
[root@localhost ~]# ifconfig eth0 192.168.2.10
[root@localhost ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0
[root@localhost ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0 broadcast 192.168.2.255

IP

配置临时有效

$ ip a                                                         # 查看IP地址
$ ip link show                     # 查看网络接口信息、网关
$ ip link set eth0 upi             # 开启网卡
$ ip link set eth0 down            # 关闭网卡
$ ip link set eth0 promisc on      # 开启网卡的混合模式
$ ip link set eth0 promisc offi    # 关闭网卡的混个模式
$ ip link set eth0 txqueuelen 1200 # 设置网卡队列长度
$ ip link set eth0 mtu 1400        # 设置网卡最大传输单元
$ ip addr show     # 查看网卡IP信息
$ ip addr add 192.168.0.1/24 dev eth0 # 设置eth0网卡IP地址192.168.0.1
$ ip addr del 192.168.0.1/24 dev eth0 # 删除eth0网卡IP地址

$ ip route show # 查看系统路由
$ ip route add default via 192.168.1.254   # 设置系统默认路由
$ ip route list                 # 查看路由信息
$ ip route add -host 192.168.10.200 gw 192.168.10.254        #指定主机路由
$ ip route add -net  192.168.4.0/24  gw  192.168.0.254    #指定网段路由
$ ip route add 192.168.4.0/24  via  192.168.0.254 dev eth0 # 设置192.168.4.0网段的网关为192.168.0.254,数据走eth0接口
$ ip route add default via  192.168.0.254  dev eth0        # 设置默认网关为192.168.0.254
$ ip route del 192.168.4.0/24   # 删除192.168.4.0网段的网关
$ ip route del default          # 删除默认路由
$ ip route delete 192.168.1.0/24 dev eth0 # 删除路由

DNS

$ cat /etc/resolv.conf             # 查看DNS,文件由网络管理服务生成,手动修改仅临时有效

NMCLI

$ nmcli general                                         # 显示一般状态信息
$ nmcli connection show            # 查看当前连接状态
$ nmcli connection reload          # 重启服务
$ nmcli connection show -active    # 显示活动的连接
$ nmcli connection show "lan eth0" # 显示指定一个网络连接配置
$ nmcli device status              # 显示设备状态
$ nmcli device show eno16777736    # 显示指定接口属性
$ nmcli device show                # 显示全部接口属性
$ nmcli device disconnect ens160     # 断开网络链接
$ nmcli dev con ens224                         # 激活网络链接,会自动给该网卡生成一个同名的配置文件,并且DHCP
$ nmcli con up static              # 启用static连接配置
$ nmcli con up default             # 启用default连接配置 
$ nmcli con add help               # 查看帮助
$ nmcli connection show --active
$ nmcli connection add type ethernet ifname ens160 con-name test-ens160  ipv4.method manual ipv4.addresses 192.168.10.80/24 ipv4.gateway 192.168.10.2 ipv4.dns 202.103.44.150 autoconnect yes   

$ nmcli connection modify ens160ipv4.method auto  # 将连接设置为使用 DHCP
$ nmcli connection modify ens224 i fname ens160   # 把ens224的配置给ens160使用
$ nmcli connection edit         # 交互式编辑器

配置文件

/etc/NetworkManager/system-connections

Hosts

[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.1  www.caoshaoqing.top    www
192.168.1.2  mail.caoshaoqing.top   mail
  • Linux服务器内核需要支持8021q模块;
$ modprobe 8021q         #加载8021q模块
$ lsmod | grep -i 8021q  #验证
 8021q                  40960  0
 garp                   16384  1 8021q
 mrp                    20480  1 8021q
  • 分别创建两个vlan接口:
nmcli connection add type vlan con-name ens32.20 dev ens32 id 20
nmcli connection add type vlan con-name ens32.30 dev ens32 id 30
  • 配置子接口ens32.20
$ nmcli connection modify ens32.20 ipv4.addresses 192.168.20.101/24 ipv4.gateway 192.168.20.1 ipv4.method manual connection.autoconnect yes
$ nmcli connection down ens32.20
$ nmcli connection up ens32.20
  • 配置子接口ens32.30
$ nmcli connection modify ens32.30 ipv4.addresses 192.168.30.101/24 ipv4.gateway 192.168.30.1 ipv4.method manual connection.autoconnect yes
$ nmcli connection down ens32.30
$ nmcli connection up ens32.30
  • 修改主配置文件
$ vi /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
BOOTPROTO=none
DEVICE=ens32
ONBOOT=yes
  • 创建并修改子接口ens32.20
$ vi /etc/sysconfig/network-scripts/ifcfg-ens32.20

DEVICE=ens32.20
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.20.101
PREFIX=24
NETWORK=192.168.20.0
VLAN=yes
  • 创建并修改子接口ens32.30
$ vi /etc/sysconfig/network-scripts/ifcfg-ens32.30

DEVICE=ens32.30BOOTPROTO=none
ONBOOT=yesIPADDR=192.168.30.101
PREFIX=24
NETWORK=192.168.30.0
VLAN=yes
  • 重启网络服务
$ systemctl restart network