标签搜索

目 录CONTENT

文章目录

派 · 折腾笔记-通过iptables配置旁路网关功能

沙漠渔
2024-01-10 16:51:35 / 0 评论 / 0 点赞 / 235 阅读 / 3,993 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-01-10,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

为了方便上网,不用通过代理方式,于是想着是不是可以通过配置旁路网关的方式实现处理请求,直接进行数据转发

大部分网络上的处理方式都是两个网卡,一个内网,一个外网,将内网的请求转发到外网,实现代理上网,我这里开发板只有一个网卡,无线连接之后会自动断开有线连接,所以通过其他方式处理。

首先确保iptables没有其他默认的配置
执行如下命令清空原有配置

sudo iptables -t nat -F
sudo iptables -t nat -X

配置下面内容

sudo iptables -P FORWARD ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT

开启DNAT模式,这里ens33配置成对应的网卡信息

iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE

配置对应的规则,22端口或者指定访问IP的直接接受,不然会出现无法连接开发板的情况,当然还可以通过tty再进去修改了再连接。
iptables -t nat -A PREROUTING -p tcp --dport 22 -j ACCEPT
配置转发规则,排除指定网卡的请求指定ip的请求都走代理通道,具体通道自行配置。
iptables -t nat -A PREROUTING -i eth0 ! -d 192.168.0.106 -i ! lo -p tcp -j DNAT --to-destination 127.0.0.1:9999

iptables的使用说明:

iptables v1.8.7

Usage: iptables -[ACD] chain rule-specification [options]
     iptables -I chain [rulenum] rule-specification [options]
     iptables -R chain rulenum rule-specification [options]
     iptables -D chain rulenum [options]
     iptables -[LS] [chain [rulenum]] [options]
     iptables -[FZ] [chain] [options]
     iptables -[NX] chain
     iptables -E old-chain-name new-chain-name
     iptables -P chain target [options]
     iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain          Append to chain
  --check   -C chain          Check for the existence of a rule
  --delete  -D chain          Delete matching rule from chain
  --delete  -D chain rulenum
                    Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                    Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                    Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                    List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                    Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                    Zero counters in chain or all chains
  --new     -N chain          Create a new user-defined chain
  --delete-chain
          -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                    Change policy on chain to target
  --rename-chain
          -E old-chain new-chain
                    Change chain name, (moving any references)
Options:
    --ipv4     -4          Nothing (line is ignored by ip6tables-restore)
    --ipv6     -6          Error (line is ignored by iptables-restore)
[!] --proto     -p proto     protocol: by number or name, eg. `tcp'
[!] --source     -s address[/mask][...]
                    source specification
[!] --destination -d address[/mask][...]
                    destination specification
[!] --in-interface -i input name[+]
                    network interface name ([+] for wildcard)
--jump     -j target
                    target for rule (may load target extension)
  --goto      -g chain
                      jump to chain with no return
  --match     -m match
                    extended match (may load extension)
  --numeric     -n          numeric output of addresses and ports
[!] --out-interface -o output name[+]
                    network interface name ([+] for wildcard)
  --table     -t table     table to manipulate (default: `filter')
  --verbose     -v          verbose mode
  --wait     -w [seconds]     maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]     wait time to try to acquire xtables lock
                    default is 1 second
  --line-numbers          print line numbers when listing
  --exact     -x          expand numbers (display exact values)
[!] --fragment     -f          match second or further fragments only
  --modprobe=<command>          try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version     -V          print package version.
0
广告 广告

评论区