树莓派配置静态IP地址,或者禁止掉网卡通过DHCP申请IP地址

树莓派默认开启了dhchcd服务,在操作系统启动或者网卡(IF)up的时候,dhcpcd会默认通过DHCP服务,给每个网卡(端口)向网卡(端口)所连接的网络的DHCP服务器去申请IP地址。

所以当我们要给树莓派的某一个或者某一些网卡设置静态IP地址时,只需要修改dhcpcd的配置文件,让dhcpcd不要去DHCP服务器申请动态IP,而是用静态IP地址直接配置网卡则可。

1、 通过dhcpcd配置

dhcpcd的配置文件放在/etc/dhcpcd.conf文件里,所以只要修改这个配置文件就可以达到我们的目标:

1.1、配置成静态IP

命令:

sudo vi /etc/dhcpcd.conf

文件里增加如下部分(把网卡名字,IP和网关地址换成你自己想要配置的网卡的名字及期IP地址):

interface eth0
static ip_address=192.168.3.66/24
static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=192.168.3.1

1.2、仅仅禁止DHCP,但不配置静态IP

命令:

sudo vi /etc/dhcpcd.conf

文件里增加如下部分(把网卡名字换成你自己想要的网卡名字):

denyinterfaces eth0

2、通过systemd-networkd配置

在/etc/systemd/network下增加一个文件36-eth0-static-ip.network,

xxx@raspberrypi:/etc/systemd/network $ ls -l
total 8
-rw-r--r-- 1 root root 85 Apr 20 18:42 36-eth0-static-ip.network
lrwxrwxrwx 1 root root  9 Feb 21 09:04 73-usb-net-by-mac.link -> /dev/null
lrwxrwxrwx 1 root root  9 Feb 21 09:04 99-default.link -> /dev/null

通过vi或其它文本编辑器,在这个文件中写入以下内容:

[Match]
Name=eth0

[Network]
Address=192.168.3.66/24
Gateway=192.168.3.1
DNS=8.8.8.8

猜你喜欢

转载自blog.csdn.net/meihualing/article/details/130265032