当前位置: 代码迷 >> 综合 >> 网络服务(4)——以太网配置IP、网关
  详细解决方案

网络服务(4)——以太网配置IP、网关

热度:95   发布时间:2024-01-10 21:38:09.0

以太网配置方法

buildroot

/etc/network/interfaces

自动分配IP

auto eth0
iface eth0 inet dhcp

(当前使用的系统在配置了dhcp之后,如果获取不到IP,会影响到桌面显示)

静态IP

auto eth0
iface eth0 inet static
address 192.168.0.232
gateway 192.168.0.1
netmask 255.255.255.0

buildroot源码修改patch

diff --git a/buildroot/package/ifupdown-scripts/ifupdown-scripts.mk b/buildroot/package/ifupdown-scripts/ifupdown-scripts.mk
index fc4d51bda..52d0b803b 100644
--- a/buildroot/package/ifupdown-scripts/ifupdown-scripts.mk
+++ b/buildroot/package/ifupdown-scripts/ifupdown-scripts.mk
@@ -8,6 +8,12 @@ define IFUPDOWN_SCRIPTS_LOCALHOST( \echo "# interface file auto-generated by buildroot"; \echo ; \
+               echo "auto eth0"; \
+               echo "iface eth0 inet static"; \
+               echo "address 192.168.0.232"; \
+               echo "gateway 192.168.0.1"; \
+               echo "netmask 255.255.255.0"; \
+               echo ; \echo "auto lo"; \echo "iface lo inet loopback"; \) > $(TARGET_DIR)/etc/network/interfaces

ifconfig指令

设置IP 地址
ifconfig eth0 192.168.1.120修改mac 地址
ifconfig eth0 hw ether 08:90:90:90:90:90设置子网掩码
ifconfig eth0 netmask 255.255.255.0设置广播地址
ifconfig eth0 broadcast 192.168.1.120添加默认网关
route add default gw 192.168.1.1删除默认网关
route del default gw 192.168.1.1

ubuntu & debian

参考链接:
https://blog.csdn.net/qq_18376583/article/details/128353450
https://blog.csdn.net/banyejiu/article/details/114433836
https://blog.csdn.net/weixin_43669941/article/details/107893322

nmcli

参考链接:https://blog.csdn.net/wq1205750492/article/details/124497231红帽、CentOS 7、Ubuntu、debian等系统中默认的网络服务可以通过 NetworkManager管理。这是动态控制及配置网络的守护进程,它用于保持当前网络设备及连接处于工作状态,同时也支持传统的 ifcfg 类型的配置文件。NetworkManager 可以管理多种网络类型:Ethernet,VLANS,Bridges,Wi-Fi,移动网络等。NetworkManager 可以配置网络别名,IP 地址,静态路由,DNS,VPN以及很多其它的特殊参数。对应的网络管理命令行工具,nmcli – command-line tool for controlling NetworkManager。

常用指令:
1、nmcli:查看网络接口ip等信息

linaro@linaro-alip:~$ nmcli
eth0: 连接中(正在获取 IP 配置) to eth0"eth0"ethernet (rk_gmac-dwmac), 0A:CE:C0:5A:A1:56, 硬件, mtu 1500eth1: 不可用"eth1"ethernet (rk_gmac-dwmac), D2:8A:B5:89:42:A6, 硬件, mtu 1500can0: 未托管"can0"can (rockchip_canfd), 硬件, mtu 16can1: 未托管"can1"can (rockchip_canfd), 硬件, mtu 16lo: 未托管"lo"loopback (unknown), 00:00:00:00:00:00, 软件, mtu 65536使用 "nmcli device show" 获取关于已知设备的完整信息,以及
"nmcli connection show" 获取活动连接配置集的概述。完整的用法细节,可参考 nmcli(1) 和 nmcli-examples(5) 手册页。

2、nmcli device status:查看网络设备状态

linaro@linaro-alip:~$ nmcli device status
DEVICE  TYPE      STATE   CONNECTION
eth0    ethernet  已连接  eth0
eth1    ethernet  不可用  --
can0    can       未托管  --
can1    can       未托管  --
lo      loopback  未托管  -

3、nmcli device show:查看网络设备详细信息

linaro@linaro-alip:~$ nmcli device show
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         0A:CE:C0:5A:A1:56
GENERAL.MTU:                            1500
GENERAL.STATE:                          100(已连接)
GENERAL.CONNECTION:                     eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/39
WIRED-PROPERTIES.CARRIER:               开
IP4.ADDRESS[1]:                         172.16.0.161/24
IP4.GATEWAY:                            172.16.0.1
IP4.ROUTE[1]:                           dst = 172.16.0.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 172.16.0.1, mt = 100
IP4.DNS[1]:                             8.8.8.8
IP6.ADDRESS[1]:                         fe80::428f:905b:4062:1fa4/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100GENERAL.DEVICE:                         eth1
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         D2:8A:B5:89:42:A6
GENERAL.MTU:                            1500
GENERAL.STATE:                          20(不可用)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               关GENERAL.DEVICE:                         can0
GENERAL.TYPE:                           can
GENERAL.HWADDR:                         (未知)
GENERAL.MTU:                            16
GENERAL.STATE:                          10(未托管)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.GATEWAY:                            --
IP6.GATEWAY:                            --GENERAL.DEVICE:                         can1
GENERAL.TYPE:                           can
GENERAL.HWADDR:                         (未知)
GENERAL.MTU:                            16
GENERAL.STATE:                          10(未托管)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.GATEWAY:                            --
IP6.GATEWAY:                            --GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10(未托管)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256

4、nmcli connection show:查看所有连接简略信息

linaro@linaro-alip:~$ nmcli connection show
NAME        UUID                                  TYPE      DEVICE
eth0        59294cec-d46e-48fb-bb12-c91c08afdda9  ethernet  eth0
有线连接 1  5e1c551c-e542-3d13-95df-29d6fd774267  ethernet  --
有线连接 2  6a1985a9-441d-338c-b42c-0a150cadfefd  ethernet  --

5、使用nmcli进行网络配置(以eth0为例)
添加网口配置:

动态获取IP
sudo nmcli c add type ethernet con-name eth0 ifname eth0 ipv4.method auto
静态IP
sudo nmcli c add type ethernet con-name eth0 ifname eth0 ipv4.method manual

设置IP、网关、子网掩码、DNS等

设置IP和子网掩码
sudo nmcli c modify eth0 ipv4.addr 172.16.0.161/24
设置网关
sudo nmcli c modify eth0 ipv4.gateway 172.16.0.1
删除网关
sudo nmcli c modify eth0 ipv4.gateway ""
设置DNS
sudo nmcli connection modify eth0 ipv4.dns 8.8.8.8
删除DNS
sudo nmcli connection modify eth0 -ipv4.dns 114.114.114.114 (注意减号)
配置静态路由?
nmcli connection modify eth0 +ipv4.routes "172.16.0.0/24 10.10.10.1"
设置为动态获取IP
nmcli c modify eth0 ipv4.method auto
设置为静态IP,设置静态地址之前必须先设置IP和掩码
nmcli c modify eth0 ipv4.method manual
网口关闭
nmcli c down eth0
网口开启
nmcli c up eth0

route表

https://zhuanlan.zhihu.com/p/619838356