你好,你可以 登入 weibo, github, 或 注册 成为我们的会员,来为大家分享.

Ubuntu 18.04 配置 IPtables | Louis Blog (0)

分享一下~ 也可以使用 iptables-persistent

REF https://ilouis.cn/ubuntu/ubuntu_setting_iptables.html

分享人 admin @ 2020-02-14 23:46:30

我也说两句:

登录以分享

首先配置iptables:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 1000:10000 -j DNAT --to-destina


系統爲ubuntu18.04版本:

首先配置iptables:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 1000:10000 -j DNAT --to-destination 20.20.20.1:1000-10000
iptables -t nat -A PREROUTING -p udp -m udp --dport 1000:10000 -j DNAT --to-destination 20.20.20.1:1000-10000
iptables -t nat -A POSTROUTING -d 20.20.20.1 -p tcp -m tcp --dport 1000:10000 -j SNAT --to-source 10.10.10.1
iptables -t nat -A POSTROUTING -d 20.20.20.1 -p udp -m udp --dport 1000:10000 -j SNAT --to-source 10.10.10.1

查看配置:iptables -L -t nat

保存iptables配置到文件:/etc/iptables.rules

iptables-save > /etc/iptables.rules

重啓系統後,使配置生效,需要運行命令

iptables-restore < /etc/iptables.rules

可能由於系統環境,以下兩種方式失敗:

在/etc/network/interfaces文件中添加iptables-restore < /etc/iptables.rules命令,重啓系統,iptables配置未生效

在/etc/network/if-pre-up.d/中添加腳本的方式,重啓系統,iptables配置依然失敗

成功解決方法:

vim /etc/rc.local

#!/bin/sh
iptables-restore < /etc/iptables.rules

chmod +x /etc/rc.local

vim /lib/systemd/system/rc.local.service(實例系統已經配置了這個文件,如果沒有,請手動配置)

#  SPDX-License-Identifier: LGPL-2.1+
# 
#  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

systemctl enable rc.local

重啓系統測試,iptables配置生效,問題解決

參考文章:https://ilouis.cn/ubuntu/ubuntu_setting_iptables.html

sudo apt install iptables-persistent
sudo dpkg-reconfigure iptables-persistent