文章目录
Tips:按网上的教程,常见会出现如下报错
问题现象一:(原因是系统存在多版本xtables模块,新iptables引用了老的模块)
root@localhost:~# iptables -v
iptables: symbol lookup error: iptables: undefined symbol: xtables_announce_chain
# 解决思路:
# 查看当前iptables引用的xtables模块路径
root@localhost:~# ldd $(which iptables)
linux-vdso.so.1 (0x0000007faf7aa000)
libxtables.so.12 => /lib/aarch64-linux-gnu/libxtables.so.12 (0x0000007faf6cd000) # 注意就是这行,注意这个路径的是老的,会造成上面的问题
libip4tc.so.2 => /lib/aarch64-linux-gnu/libip4tc.so.2 (0x0000007faf6b5000)
libip6tc.so.2 => /lib/aarch64-linux-gnu/libip6tc.so.2 (0x0000007faf69d000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007faf529000)
libdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000007faf515000)
/lib/ld-linux-aarch64.so.1 (0x0000007faf77a000)
# 按名称查找是否有其他路径(你大约会在 /usr/local/lib/ 目录下看到有一个模块,这就是手动编译的最新的)
find /usr/ -name libxtables.so*
# 删除老的xtables模块,重新自动配置xtables模块,然后再验证是否指向正确
rm -rf /lib/aarch64-linux-gnu/libxtables.so ; ldconfig
root@localhost:~# ldd $(which iptables)
linux-vdso.so.1 (0x0000007fb39c5000)
libxtables.so.12 => /usr/local/lib/libxtables.so.12 (0x0000007fb38c0000)
libip4tc.so.2 => /usr/local/lib/libip4tc.so.2 (0x0000007fb38a0000)
libip6tc.so.2 => /usr/local/lib/libip6tc.so.2 (0x0000007fb3880000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007fb36d0000)
/lib/ld-linux-aarch64.so.1 (0x0000007fb398c000)
问题现象二:(原因是上面删除老的xtables模块后,没找到新的xtables模块并关联)
root@localhost:~# iptables --version
iptables: symbol lookup error: iptables: undefined symbol: xt_xlate_get_family
附:/etc/ld.so.conf.d/ 目录下文件,配置了内核库模块的引用路径,
root@localhost:~# cat /etc/ld.so.conf.d/aarch64-linux-gnu.conf
# Multiarch support
/usr/local/lib/aarch64-linux-gnu
/lib/aarch64-linux-gnu
/usr/lib/aarch64-linux-gnu
root@localhost:~# cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib
附:netfilter网站 https://www.netfilter.org/index.html
ubuntu22.04 如何将原版iptables的nf_tables切换成legacy模式
第一:查看当前 iptables 后端模式
root@localhost:~# update-alternatives --display iptables
iptables - auto mode
link best version is /usr/sbin/iptables-nft
link currently points to /usr/sbin/iptables-nft # 表示当前是这种模式,是自动的,他认为这是最优的
link iptables is /usr/sbin/iptables
slave iptables-restore is /usr/sbin/iptables-restore
slave iptables-save is /usr/sbin/iptables-save
/usr/sbin/iptables-legacy - priority 10
slave iptables-restore: /usr/sbin/iptables-legacy-restore
slave iptables-save: /usr/sbin/iptables-legacy-save
/usr/sbin/iptables-nft - priority 20
slave iptables-restore: /usr/sbin/iptables-nft-restore
slave iptables-save: /usr/sbin/iptables-nft-save
第二:切换成legacy模式
root@localhost:~# update-alternatives --set iptables /usr/sbin/iptables-legacy
root@localhost:~# iptables --version
iptables v1.8.7 (legacy)
第三:同样ip6tables和arptables也可以切换
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-alternatives --set arptables /usr/sbin/arptables-legacy # 若是报错,可能因为没装,你需要 apt install arptables
第四:切回nf_tables模式
# apt安装的(原生版本的切换)
update-alternatives --set iptables /usr/sbin/iptables-nft
update-alternatives --set ip6tables /usr/sbin/ip6tables-nft
update-alternatives --set arptables /usr/sbin/arptables-nft
# 手动默认编译的切换
update-alternatives --set iptables /usr/local/sbin/iptables-nft
update-alternatives --set ip6tables /usr/local/sbin/ip6tables-nft
update-alternatives --set arptables /usr/local/sbin/arptables-nft
附、iptables注册信息操作方式
# 查看当前iptables的注册信息(若是没有/usr/local/sbin/就需要手动注册)
update-alternatives --display iptables
# 移除老的注册
update-alternatives --remove iptables /usr/sbin/iptables-nft
update-alternatives --remove iptables /usr/sbin/iptables-legacy
update-alternatives --remove ip6tables /usr/sbin/ip6tables-nft
update-alternatives --remove ip6tables /usr/sbin/ip6tables-legacy
update-alternatives --remove arptables /usr/sbin/arptables-nft
# 注册新的iptables信息(注意 数字 优先级)
update-alternatives --install /usr/local/sbin/iptables iptables /usr/local/sbin/iptables-legacy 10 \
--slave /usr/local/sbin/iptables-save iptables-save /usr/local/sbin/iptables-legacy-save \
--slave /usr/local/sbin/iptables-restore iptables-restore /usr/local/sbin/iptables-legacy-restore
update-alternatives --install /usr/local/sbin/iptables iptables /usr/local/sbin/iptables-nft 20 \
--slave /usr/local/sbin/iptables-save iptables-save /usr/local/sbin/iptables-nft-save \
--slave /usr/local/sbin/iptables-restore iptables-restore /usr/local/sbin/iptables-nft-restore
# 设置新的候选项为默认
update-alternatives --set iptables /usr/local/sbin/iptables-nft
附:ubuntu22.04-arm64升级iptables到v1.8.10脚本
#!/bin/bash
## Author: LT
## Date: 2025-01-02
## 作用范围:将arm64架构的 ubuntu22.04 的iptables 升级为 iptables 1.8.10
## 判断是否具有root权限
[[ $EUID -ne 0 ]] && echo "请使用root账户运行该脚本" 1>&2 && exit 1
## 重新定义颜色输出(新命令附带-e指标)
echored() { echo -e $'\e[0;31m'"$1"$'\e[0m'; }
echogreen() { echo -e $'\e[0;32m'"$1"$'\e[0m'; }
echoyellow() { echo -e $'\e[0;33m'"$1"$'\e[0m'; }
## 定义版本信息、定义日志相关
CTIME=$(date "+%Y-%m-%d %H:%M:%S")
LOG="/tmp/sysinit.log"
## 检测并重新定义下载工具命令(LTGET用法介绍)
# LTGET newfilename http://xxx/原始文件名 or LTGET /tmp/newfilename http://xxx/原始文件名
if command -v wget &> /dev/null; then
LTGET="wget -4 -q -O"
elif command -v curl &> /dev/null; then
LTGET="curl -4 -s -o"
else
echo "系统未安装 wget 或 curl,请先安装其中一个工具。"
exit 1
fi
function get_os() {
# 收集OS发行版分支、及分支版本
if [ -f /etc/os-release ]; then
source /etc/os-release
os_name=${NAME}
os_release=${ID}
os_version_id=${VERSION_ID}
os_version_name=${VERSION_CODENAME}
else
echo "未知OS发行版、程序已退出"
exit 1
fi
# 收集OS内核信息
kernel_version=$(uname -r | awk -F "-" '{print $1}')
kernel_version_full=$(uname -r)
major_version=$(echo $kernel_version_full | cut -d. -f1)
# 打印系统信息到屏幕,并追加到日志中
echogreen "${CTIME} |发行版:${os_name} |发行分支:${os_release} |分支版本号:${os_version_id} |分支代号:${os_version_name}" | tee -a ${LOG}
echogreen "${CTIME} |内核版本号:${kernel_version} |内核主版本号:${major_version} |内核版本详细信息:${kernel_version_full}" | tee -a ${LOG}
# 判断cpu架构是否符合要求(只允许arm64执行)
[ `uname -m` == "aarch64" ] && { echogreen "${CTIME} |您的CPU架构是arm64 在升级支持范围内" | tee -a ${LOG} ;} || { echored "${CTIME} |您的CPU架构不是arm64 不在升级支持范围内" | tee -a ${LOG} ; exit 1; }
# 判断操作系统是否符合要求
if [[ "${os_release}" == "ubuntu" && "${os_version_id}" == "22.04" ]]; then
echogreen "${CTIME} |您的系统符合升级需求" | tee -a ${LOG}
#elif [[ "${os_release}" == "debian" || "${os_release}" == "ubuntu" ]]; then
# echogreen "${CTIME} |您的系统满足安装要求" | tee -a ${LOG}
else
echored "${CTIME} 您的系统不满足安装要求" | tee -a ${LOG}
exit 1
fi
}
## 检查iptables版本是否需要升级
[[ `iptables --version` == "iptables v1.8.10 (legacy)" ]] && { echogreen "${CTIME} |iptables 满足要求,无需升级" | tee -a ${LOG} ;exit 0;} || { echogreen "${CTIME} |正在为您升级 iptables 请稍等" | tee -a ${LOG}; }
## 备份iptables配置
function backup_rule() {
iptables-save > /home/iptables_rule.bak
ip6tables-save > /home/ip6tables_rule.bak
}
## 安装必要的软件
function configure_dev_environment() {
# pkg-config 会自动配置环境
apt update ; apt install -y pkg-config
apt install -y make gcc g++
# apt install libmnl-dev libnftnl-dev libxtables-dev libip6tc-dev libip4tc-dev
# apt install -y xtables-addons-common xtables-addons-dkms
# 下载软件包
cd /root;
$LTGET libmnl-1.0.5.tar.bz2 https://www.netfilter.org/projects/libmnl/files/libmnl-1.0.5.tar.bz2
$LTGET libnftnl-1.2.7.tar.xz https://www.netfilter.org/projects/libnftnl/files/libnftnl-1.2.7.tar.xz
$LTGET iptables-1.8.10.tar.xz https://www.netfilter.org/projects/iptables/files/iptables-1.8.10.tar.xz
#$LTGET xtables-addons-3.26.tar.xz https://inai.de/files/xtables-addons/xtables-addons-3.26.tar.xz
tar xf libmnl-1.0.5.tar.bz2 ; rm -rf libmnl-1.0.5.tar.bz2
tar xf libnftnl-1.2.7.tar.xz ; rm -rf libnftnl-1.2.7.tar.xz
tar xf iptables-1.8.10.tar.xz ; rm -rf iptables-1.8.10.tar.xz
#tar xf xtables-addons-3.26.tar.xz ; rm -rf xtables-addons-3.26.tar.xz
# 彻底卸载老版本iptables(会自动卸载ip6tables、arptables、ufw、关键是也会自动卸载apt安装的docker,踩过坑)
#apt autoremove -y iptables
cd /root/libmnl-1.0.5; ./configure ; make ; make install
cd /root/libnftnl-1.2.7 ; ./configure ; make ; make install
cd /root/iptables-1.8.10 ; ./configure --enable-static --enable-shared --enable-libipq ; make ; make install
#cd /root/xtables-addons-3.26 ; ./configure ; make ; make install
cd /root ; rm -rf /root/libmnl-1.0.5 ; rm -rf /root/libnftnl-1.2.7 ; rm -rf /root/iptables-1.8.10 ; #rm -rf /root/xtables-addons-3.26
# 删除残留的老版本库(若是存在会无法使用iptables)
rm -rf /usr/lib/aarch64-linux-gnu/libxtables.so*
rm -rf /usr/lib/aarch64-linux-gnu/libip4tc.so*
rm -rf /usr/lib/aarch64-linux-gnu/libip6tc.so* ; rm -rf /lib/aarch64-linux-gnu/libip6tc.so*
# 更新新链接库
ldconfig
# 更新环境变量再执行版本查看,(Tips:若不更新环境变量,当前脚本可能使用老的/usr/sbin/iptables 执行,这肯定会出错,新编译安装的地址是/usr/local/sbin)
source /etc/environment
# 注意这里的iptables --version在source 环境变量后,其实/usr/local/sbin/ 优先 /usr/sbin/ (因为/etc/environment 文件中有先顺序) (若你不放心 可以写成/usr/local/sbin/iptables --version )
if [[ `/usr/local/sbin/iptables --version` == "iptables v1.8.10 (legacy)" ]];then
echogreen "${CTIME} |iptables 升级成功" | tee -a ${LOG}
# 更新iptables注册信息, update-alternatives(查询命令:update-alternatives --display iptables ; update-alternatives --display ip6tables ;update-alternatives --display arptable)
# 若是开始前卸载了老的iptables,直接就--install,不需要--remove(我这里老版本还在,所以要先--remove)
update-alternatives --remove iptables /usr/sbin/iptables-nft
update-alternatives --remove iptables /usr/sbin/iptables-legacy
update-alternatives --remove ip6tables /usr/sbin/ip6tables-nft
update-alternatives --remove ip6tables /usr/sbin/ip6tables-legacy
update-alternatives --remove arptables /usr/sbin/arptables-nft
update-alternatives --install /usr/local/sbin/iptables iptables /usr/local/sbin/iptables-legacy 10 \
--slave /usr/local/sbin/iptables-save iptables-save /usr/local/sbin/iptables-legacy-save \
--slave /usr/local/sbin/iptables-restore iptables-restore /usr/local/sbin/iptables-legacy-restore
update-alternatives --install /usr/local/sbin/iptables iptables /usr/local/sbin/iptables-nft 20 \
--slave /usr/local/sbin/iptables-save iptables-save /usr/local/sbin/iptables-nft-save \
--slave /usr/local/sbin/iptables-restore iptables-restore /usr/local/sbin/iptables-nft-restore
update-alternatives --install /usr/local/sbin/ip6tables ip6tables /usr/local/sbin/ip6tables-legacy 10 \
--slave /usr/local/sbin/ip6tables-save ip6tables-save /usr/local/sbin/ip6tables-legacy-save \
--slave /usr/local/sbin/ip6tables-restore ip6tables-restore /usr/local/sbin/ip6tables-legacy-restore
update-alternatives --install /usr/local/sbin/ip6tables ip6tables /usr/local/sbin/ip6tables-nft 20 \
--slave /usr/local/sbin/ip6tables-save ip6tables-save /usr/local/sbin/ip6tables-nft-save \
--slave /usr/local/sbin/ip6tables-restore ip6tables-restore /usr/local/sbin/ip6tables-nft-restore
update-alternatives --install /usr/local/sbin/arptables arptables /usr/local/sbin/arptables-nft 10 \
--slave /usr/local/sbin/arptables-save arptables-save /usr/local/sbin/arptables-nft-save \
--slave /usr/local/sbin/arptables-restore arptables-restore /usr/local/sbin/arptables-nft-restore
[ $? -eq 0 ] && { echogreen "${CTIME} |iptables 注册信息更新成功" | tee -a ${LOG} ;} || { echored "${CTIME} |iptables 注册信息更新失败" | tee -a ${LOG} ;}
update-alternatives --set iptables /usr/local/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/local/sbin/ip6tables-legacy
else
echored "${CTIME} |iptables 升级失败" | tee -a ${LOG}
exit 1
fi
}
## 还原iptables配置
function restore_rule() {
iptables-restore < /home/iptables_rule.bak
ip6tables-restore < /home/ip6tables_rule.bak
# 重启docker(原因是docker启动的时候就使用了iptables,当时版本是老的,所以要重启使用新的来加载)
systemctl restart docker &> /dev/null
}
get_os
backup_rule
configure_dev_environment
restore_rule
如果文章对你有帮助,欢迎点击上方按钮打赏作者
暂无评论