Configure a static IP address for the Raspberry Pi, or disable the network card to apply for an IP address through DHCP

The Raspberry Pi enables the dhchcd service by default. When the operating system is started or the network card (IF) is up, dhcpcd will use the DHCP service by default to apply for each network card (port) to the DHCP server of the network connected to the network card (port). IP address.

So when we want to set a static IP address for one or some network cards of the Raspberry Pi, we only need to modify the configuration file of dhcpcd, so that dhcpcd does not go to the DHCP server to apply for a dynamic IP, but directly configures the network card with a static IP address. .

1. Configure through dhcpcd

The configuration file of dhcpcd is placed in the /etc/dhcpcd.conf file, so we can achieve our goal by modifying this configuration file:

1.1, configure static IP

Order:

sudo vi /etc/dhcpcd.conf

Add the following parts to the file (replace the network card name, IP and gateway address with the name and IP address of the network card you want to configure):

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. Only prohibit DHCP, but do not configure static IP

Order:

sudo vi /etc/dhcpcd.conf

Add the following part to the file (replace the name of the network card with the name of the network card you want):

denyinterfaces eth0

2. Configure through systemd-networkd

Add a file 36-eth0-static-ip.network under /etc/systemd/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

With vi or other text editor, write the following content in this file:

[Match]
Name=eth0

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

Guess you like

Origin blog.csdn.net/meihualing/article/details/130265032