LoginSignup
0
0

More than 3 years have passed since last update.

linuxのiptablesでproxyサーバーを簡単に実現する方法

Posted at

概要

linuxのiptablesでproxyサーバーを簡単に実現します。ローカルネットワーク内のpc1は、proxyサーバーを経由して、外部インターネットにアクセスします。

pc1(public ipなし) ----> proxy server(public ipあり) ---> インターネット

##pc1##
private ip: 172.31.8.4

##proxy server##
private ip: 172.31.11.10
auto public ip: 13.231.116.234

環境

  • AWS VPC
  • AWS EC2 (proxyサーバ:1台、pc1:1台)

    • Debian 10 (HVM), SSD Volume Type - ami-0ac97798ccf296e02 (64-bit x86)
    • t2.micro

手順

1. AWS EC2インスタンスの作成(proxyサーバ)

Auto-assign Public IP:Enable
送信元/送信先チェックを無効にする
作成したEC2インスタンスを選択して --> Actions --> Networking --> Change source/destination check --> Stopにチェックを入れて、Saveボタンを押下します。
IP forwardを有効にする

admin@ip-172-31-11-10:~$ cat <<EOF | sudo tee /etc/sysctl.d/ipforward.conf
> net.ipv4.ip_forward=1
> EOF
net.ipv4.ip_forward=1
admin@ip-172-31-11-10:~$ 
admin@ip-172-31-11-10:~$ sudo sysctl --system
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/ipforward.conf ...
net.ipv4.ip_forward = 1
* Applying /etc/sysctl.d/protect-links.conf ...
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.conf ...
admin@ip-172-31-11-10:~$ 

iptablesにルールを追加する(source ip: 172.31.8.4 --> 172.31.11.10)

〜〜外部に送信する前に、source ip(pc1のip)を自動的にproxyサーバのeth0のipに設定する〜〜
admin@ip-172-31-11-10:~$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
admin@ip-172-31-11-10:~$ 
admin@ip-172-31-11-10:~$ sudo iptables -t nat -S
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P POSTROUTING ACCEPT
-P OUTPUT ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
admin@ip-172-31-11-10:~$

2. AWS EC2インスタンスの作成(pc1)

Auto-assign Public IP:Disable
Kernel IP routing tableを変更する

〜〜default Gatewayを削除する〜〜
admin@ip-172-31-8-4:~$ sudo ip route del default
〜〜新しいdefault Gateway(proxyサーバー)を追加する〜〜
admin@ip-172-31-8-4:~$ sudo ip route add default via 172.31.11.10 dev eth0

変更前:

admin@ip-172-31-8-4:~$ sudo route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.31.0.1      0.0.0.0         UG    0      0        0 eth0
172.31.0.0      0.0.0.0         255.255.240.0   U     0      0        0 eth0
admin@ip-172-31-8-4:~$ 

変更後:

admin@ip-172-31-8-4:~$ sudo route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.31.11.10    0.0.0.0         UG    0      0        0 eth0
172.31.0.0      0.0.0.0         255.255.240.0   U     0      0        0 eth0
admin@ip-172-31-8-4:~$ 

3.結果を検証する

admin@ip-172-31-8-4:~$ curl yahoo.co.jp
<HTML>
<HEAD>
<TITLE>Document Has Moved</TITLE>
</HEAD>

<BODY BGCOLOR="white" FGCOLOR="black">
<H1>Document Has Moved</H1>
<HR>

<FONT FACE="Helvetica,Arial"><B>
Description: The document you requested has moved to a new location.  The new location is "https://www.yahoo.co.jp/".
</B></FONT>
<HR>
</BODY>
admin@ip-172-31-8-4:~$ 
admin@ip-172-31-8-4:~$ 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0