ip/ifconfig 工具介绍通过命令执行的方式直接管理网卡优点就是快、参数立即生效缺点就是重启系统参数全丢又需要重新配置。配置文件无动态 IP借助 ifconfig 配置网卡ifconfig eth0 0.0.0.0 # 1. 释放并清空网卡当前的 IPdhclient eth0 # 2. 调用 dhclient 协议向网络索要 IP借助 ip 配置网卡ip addr flush dev eth0 # 1. 释放并清空网卡当前的 IPdhclient eth0 # 2. 调用 dhclient 协议向网络索要 IP静态 IP借助 ifconfig 配置网卡ifconfig eth0 192.168.1.100 netmask 255.255.255.0 # 同时配置 IP 和 子网掩码route add default gw 192.168.1.1 eth0 # 配置默认网关ifconfig 自身不能配网关必须借助 route 命令借助 ip 配置网卡ip addr add 192.168.1.100/24 dev eth0 # 同时配置 IP 和 子网掩码ip route add default via 192.168.1.1 dev eth0 # 配置默认网关ip 自身不能配网关必须借助 route 命令启停命令借助 ifconfig 启停网卡ifconfig eth0 up # 启动网卡ifconfig eth0 down # 关闭网卡借助 ip 启停网卡ip link set eth0 up # 启动网卡ip link set eth0 down # 关闭网卡2、ifcfg工具介绍通过脚本解析配置文件的方式管理网卡每一个网卡均对应着一个文件配置文件简单易读。配置文件/etc/sysconfig/├── network -- 全局网络配置文件如主机名、全局网关└── network-scripts/ -- ifcfg 机制的核心目录最重要├── ifcfg-eth0 -- 网卡 eth0 的具体配置文件核心├── ifcfg-lo -- 本地环回接口的配置文件├── ifdown - ./ifdown-eth -- 符号链接指向具体的关闭脚本├── ifdown-eth -- 关闭以太网卡的底层脚本├── ifdown-post -- 网卡关闭后执行的清理脚本├── ifup - ./ifup-eth -- 符号链接指向具体的启动脚本├── ifup-eth -- 启动以太网卡的底层脚本└── ifup-post -- 网卡启动后执行的后处理脚本动态 IPcat /etc/sysconfig/network-scripts/ifcfg-eth0DEVICEeth0TYPEEthernetONBOOTyesBOOTPROTOdhcp静态 IPcat /etc/sysconfig/network-scripts/ifcfg-eth0DEVICEeth0TYPEEthernetONBOOTyesBOOTPROTOstaticIPADDR192.168.1.100NETMASK255.255.255.0GATEWAY192.168.1.1DNS18.8.8.8DNS21.1.1.1启停命令启动单个网卡ifup eth0关闭单个网卡ifdown eth0重启整个网络的所有网卡/etc/init.d/network restart3、ifupdown工具介绍通过脚本解析配置文件的方式管理网卡可以通过一个文件集中管理所有网卡的配置信息也可以分割开来单独管理灵活性较高。【注可以将其看作是 ifcfg 的升级版因为两者的区别仅仅在于配置文件参数格式的不同启停网卡以及目录结构上都非常的相像。】目录结构/etc/network/├── interfaces -- 主配置文件核心├── interfaces.d/ -- 用户自定义子配置目录常用于模块化管理│ ├── eth0│ └── wlan0├── if-pre-up.d/ -- 网卡激活前自动执行的脚本目录├── if-up.d/ -- 网卡激活后自动执行的脚本目录├── if-down.d/ -- 网卡关闭前自动执行的脚本目录└── if-post-down.d/ -- 网卡关闭后自动执行的脚本目录注一定要检查 /etc/network/interfaces 文件中是否包含 source /etc/network/interfaces.d/*否则 interfaces.d 目录下的配置不生效。动态 IPcat /etc/network/interfaces.d/eth0auto eth0iface eth0 inet dhcp静态 IPcat /etc/network/interfaces.d/eth0auto eth0iface eth0 inet staticaddress 192.168.1.100/24gateway 192.168.1.1dns-nameservers 8.8.8.8 1.1.1.1启停命令启动单个网卡ifup eth0关闭单个网卡ifdown eth0重启整个网络的所有网卡/etc/init.d/networking restart4、NetworkManager工具介绍以服务的方式管理网卡基于此服务还可配合多种工具去管理网卡如配套的 nmcli、nmtui、或 netplan、或直接修改 /etc/NetworkManager/system-connections/eth0.nmconnection 的方式去管理网卡。配置文件/etc/NetworkManager/├── NetworkManager.conf -- 主配置文件控制守护进程自身的行为├── conf.d/ -- 主配置子的补充目录用于模块化修改主配置│ └── 90-dns-none.conf├── dispatcher.d/ -- 事件网络脚本目录类似于 ifupdown的 if-up.d│ ├── 01-ifupdown│ └── pre-up.d/├── system-connections/ -- 核心存放所有网络连接配置文件最重要│ ├── eth0.nmconnection│ └── MyWiFi.nmconnection└── dnsmasq.d/ -- 供内置 dnsmasq 插件使用的配置目录可选动态 IPcat /etc/NetworkManager/system-connections/eth0.nmconnection[connection]ideth0typeethernetinterface-nameeth0[ipv4]methodauto静态 IPcat /etc/NetworkManager/system-connections/eth0.nmconnection[connection]idstatic-eth0typeethernetinterface-nameeth0[ipv4]methodmanualaddresses192.168.1.100/24gateway192.168.1.1dns8.8.8.8;1.1.1.1;启停命令启动单个网卡nmcli device connect eth0nmcli connection up id “Wired connection 1”关闭单个网卡nmcli device disconnect eth0nmcli connection down id “Wired connection 1”列出网卡名称nmcli connection show亦可通过一行命令永久更改网卡 ipnmcli connection modify “Wired” ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv4.gateway 192.168.1.254 ipv4.dns 8.8.8.8重启整个网络的所有网卡systemctl restart NetworkManagerTUI 可视化的管理网卡nmtui