甲骨文云实例可以免费开启IPv6,在IPv4逐渐紧张的情况下,必须先做好准备,以防万一甲骨文云像亚马逊一样取消免费的IPv4。甲骨文云在面板操作分配IPv6后(参考),需要登录系统运行dhclient -6获取地址,而且重启后不能自动获取IPv6,需要重新运行该命令。本文旨在解决该问题。
提示
本文基于Debian 12 Bookworm,同样适用于 Debian 11 Bullseye, Debian 10 Buster 和 Debian 9 Strech。
开机自启动功能,debian系已经停用rc.local,切换到 systemd 了,但是对于一些简单的脚本或者懒人来说,添加命令到 /etc/rc.local 文件更方便。自从 Debian 9 开始,Debian 默认不带 /etc/rc.local 文件,而 rc.local 服务却还是自带的:
root@debian ~ # cat /lib/systemd/system/rc-local.service # SPDX-License-Identifier: LGPL-2.1-or-later # # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.local is executable. [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no
默认情况下这个服务是关闭的状态:
root@debian ~ # systemctl status rc-local ● rc-local.service - /etc/rc.local Compatibility Loaded: loaded (/lib/systemd/system/rc-local.service; static) Drop-In: /usr/lib/systemd/system/rc-local.service.d └─debian.conf Active: inactive (dead) Docs: man:systemd-rc-local-generator(8)
为了解决这个问题,我们需要手工添加一个 /etc/rc.local 文件:
cat <<EOF >/etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0 EOF
然后赋予权限:
chmod +x /etc/rc.local
接着启动 rc-local 服务:
systemctl enable --now rc-local
此时可能会弹出警告:
The unit files have no installation config (WantedBy=, RequiredBy=, Also=, Alias= settings in the [Install] section, and DefaultInstance= for template units). This means they are not meant to be enabled using systemctl. Possible reasons for having this kind of units are: • A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. • A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. • A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...). • In case of template units, the unit is meant to be enabled with some instance name specified.
无视警告,因为这个服务没有任何依赖的系统服务,只是开机启动 /etc/rc.local 脚本而已。
再次查看状态:
root@debian ~ # systemctl status rc-local.service ● rc-local.service - /etc/rc.local Compatibility Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled) Drop-In: /usr/lib/systemd/system/rc-local.service.d └─debian.conf Active: active (exited) since Thu 2022-01-27 18:52:43 UTC; 10s ago Docs: man:systemd-rc-local-generator(8) Process: 541 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS) CPU: 3ms Jan 27 18:52:43 debian systemd[1]: Starting /etc/rc.local Compatibility... Jan 27 18:52:43 debian systemd[1]: Started /etc/rc.local Compatibility.
至此可以使用rc.local开机自启动命令或脚本。
现在就可以把获取IPv6的命令 sudo dhclient -6添加到 /etc/rc.local 文件,丢在 exit 0 前面即可,并尝试reboot后运行ip a可见:
opc@debian:~$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host noprefixroute valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq state UP group default qlen 1000 link/ether 02:00:17:01:7f:e0 brd ff:ff:ff:ff:ff:ff altname enp0s3 inet 10.0.0.65/24 brd 10.0.0.255 scope global ens3 valid_lft forever preferred_lft forever inet6 2***:****:****:****:****:****:****:abcd/128 scope global dynamic valid_lft 87767sec preferred_lft 84167sec inet6 fe80::17ff:fe01:7fe0/64 scope link valid_lft forever preferred_lft forever
实现了开机自动获取IPv6地址。
本文作者:尽力局副局长
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!