使用DHCPCD网络管理器

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Network management using DHCPCD and the translation is 37% complete.
Outdated translations are marked like this.
Other languages:

本文基于发布在Gentoo论坛的帖子 并解释了如何使用dhcpcd进行完整的网络堆栈管理。

Of course, it's possible to use dhcpcd together with net.* scripts. In fact, if dhcpcd is installed, it's used as the default DHCP client if config_eth0="dhcp" is set in /etc/conf.d/net. This article is specifically about using dhcpcd standalone.

设置

确保 dhcpcd 已经安装。接下来,将它添加到默认运行级别并启动服务:

If using OpenRC, do:

root #rc-update add dhcpcd default
root #/etc/init.d/dhcpcd start

Or, if using systemd:

root #systemctl enable dhcpcd
root #systemctl start dhcpcd

此设置偏离了Gentoo手册中的默认网络说明。 它具有在有线和无线网络接口之间提供自动切换的优点。

Wireless

wpa_supplicant

wpa_supplicant can be used for authentication of wireless and wired network interfaces. Create the configuration file if it does not exist:

文件 /etc/wpa_supplicant/wpa_supplicant.conf
# Allow users in the 'wheel' group to control wpa_supplicant
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel

# Make this file writable for wpa_gui / wpa_cli
update_config=1

For authentication for the wired interface, add the configuration file mentioned in Wpa_supplicant#Setup_wired_802.1X.

First follow the setup guide for dhcpcd.

Emerge wpa_supplicant (Version >=2.6-r2 is needed in order to get the CONFIG_MATCH_IFACE option added in April 2017):

root #emerge --ask net-wireless/wpa_supplicant

Using OpenRC

Complete its conf.d file with the -M option for the wireless network interface:

文件 /etc/conf.d/wpa_supplicant
wpa_supplicant_args="-B -M -c /etc/wpa_supplicant/wpa_supplicant.conf"

In case authentication for the wired interface is needed, this configuration file should look like:

文件 /etc/conf.d/wpa_supplicant
wpa_supplicant_args="-ieth0 -Dwired -c/etc/wpa_supplicant/wpa_supplicant_wired.conf -B -M -c/etc/wpa_supplicant/wpa_supplicant.conf"

With the configuration done, run it as a service:

root #rc-update add wpa_supplicant default
root #rc-service wpa_supplicant start

Using Systemd

Systemd allows a simpler per-device setup without needing to create the above conf.d files. As explained under wpa_supplicant item in the Native services section, a service symlink such as wpa_supplicant@wlan0.service looks for a separate configuration file to manage the device wlan0 in this case.

To configure a specific device this way, first copy or rename the /etc/wpa_supplicant/wpa_supplicant.conf file as /etc/wpa_supplicant/wpa_supplicant-DEVNAME.conf where DEVNAME should be the name of the device, such as wlan0.

Then, navigate to /etc/systemd/system/multi-user.target.wants and create the symlink:

root #ln -s /lib/systemd/system/wpa_supplicant@.service wpa_supplicant@DEVNAME.service

where DEVNAME is same device name as in the conf file above.

重要
Note the @ signs on both arguments in the symlink step.

Test the system:

root #systemctl daemon-reload
root #systemctl start wpa_supplicant@DEVNAME
root #systemctl status wpa_supplicant@DEVNAME

Using net-wireless/iwd

See the iwd article.

测试

Stop dhcpcd and wpa_supplicant, then start dhcpcd with debugging enabled and in the foreground to see dhcpcd starting the connection.

root #/etc/init.d/dhcpcd stop
root #killall wpa_supplicant
root #dhcpcd -dB
dhcpcd[23772]: version 6.4.5 starting
dhcpcd[23772]: udev: starting
dhcpcd[23772]: dev: loaded udev
...
...

静态IP地址

In case the network interface card should be configured with a static IP address, type it into the graphical user interface. Without the graphical user interface, entries can also be manually added (currently offline) to /etc/dhcpcd.conf as described in Dhcpcd#Static_IP_addresses.

从 Gentoo net.* 脚本迁移

When migrating from Gentoo's net.* scripts it is essential to remove the net.* scripts from the runlevels.

user $rc-config list | grep 'wpa_supplicant\|dhcpcd\|net.'
  net.wlp8s0                default
  dhcpcd                    default

Any match starting with net. must not have a runlevel assigned. In the above example, net.wlp8s0 needs to be removed.

root #rc-update del net.wlp8s0
root #/etc/init.d/net.wlp8s0 stop

Once the network setup works well, it is possible to completely remove the old Gentoo net.* scripts. To do so, disable the netifrc useflag, re-emerge OpenRC and finally unmerge net-misc/netifrc:

root #echo 'sys-apps/openrc -netifrc' >> /etc/portage/package.use
root #emerge --ask --newuse --oneshot sys-apps/openrc
root #emerge --ask --depclean net-misc/netifrc

还可以删除旧的配置文件和符号链接:

root #rm /etc/conf.d/net
root #rm /etc/init.d/net.*

网络依赖服务

Manipulating network dependent services by dhcpcd is no longer supported since openrc-0.16.4.

图形用户界面

A dhcpcd graphical user interface is provided by net-misc/dhcpcd-ui.

另请参阅

外部资源