1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【2023年8月版】Ubuntu22.04で作る簡単Linuxルーター

Last updated at Posted at 2023-08-18

はじめに

Linuxでルーター構築する勉強したいな。。。

そうだ、ChatGPT先生に教えてもらいながら構築しよう

環境

  • Ubuntu22.04
  • インターネット側インターフェイス eth0
  • 内側インターフェイス eth1
  • 外部から内部への接続はできないようにするだけで、セキュリティは考慮せず

構築

ルーターとしての設定を保存するコマンドをインストール

$ sudo apt-get install iptables-persistent

パッケットのフォワードができるように設定

$ echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p
net.ipv4.ip_forward = 1

ルーターとして動作するように iptables を設定

# NAPT(IPマスカレード)の設定
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# 許可されたトラフィックのみ通過(eth0からeth1のすべてのフォワードは許可)
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

設定を永続化する

$ sudo sh -c 'iptables-save > /etc/iptables/rules.v4'

感想

VPNパスするーをどうするんだろうと、思ったけど、
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
で、TCP、UDP、ICMP、GRE (Protocol 47)、ESP (Protocol 50)、AH (Protocol 51)等が
転送されるため、特に以下のように開ける必要はなかった。
セキュリティ要件で、個々に許可するようなら以下のようになる。

# IPSEC
sudo iptables -A FORWARD -i eth1 -o eth0 -p 50 -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p 51 -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 500 -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 4500 -j ACCEPT

# IKEv2
# (UDP 500 と UDP 4500 は既に上述のIPSECの部分で設定済み)

# PPTP
sudo iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 1723 -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p 47 -j ACCEPT

# L2TP
sudo iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 1701 -j ACCEPT

# SSTP
sudo iptables -A FORWARD -i eth1 -o eth0 -p tcp --dport 443 -j ACCEPT

# OpenVPN
sudo iptables -A FORWARD -i eth1 -o eth0 -p udp --dport 1194 -j ACCEPT

さいごに

かんたんでしたね

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?