linuxルータ構築

Linux ルータ

CentOS7をインストールし、1つのNICに192.168.251.204 (enp3s0)、もう一つのNICにOCNから割り当てられた動的なIP (ppp0)が設定されている。

firewall-cmd --get-active-zone
firewall-cmd --zone=external --change-interface=ppp0 --permanent
firewall-cmd --zone=trusted --change-interface=enp3s0 --permanent
firewall-cmd --zone=external --add-forward-port=port=8352:proto=tcp:toport=3389:toaddr=192.168.251.152
4 --permanent
firewall-cmd --zone=trusted --add-masquerade --permanent
firewall-cmd --reload
firewall-cmd --get-active-zone
firewall-cmd --list-all --zone=trusted 
firewall-cmd --list-all --zone=external 

firewalldで制御せず、単にポートを転送するだけなら

firewall-cmd --list-all --zone=public
firewall-cmd --zone=public --add-port=2222/tcp --permanent
firewall-cmd --reload
socat tcp-listen:2222,fork tcp-connect:192.168.251.167:3389

firewalldでポート開けるには

firewall-cmd --zone=public --add-port=1187/tcp --permanent  
firewall-cmd --zone=public --add-port=10050/tcp --permanent   #zabbix用
firewall-cmd --permanent --add-service=nfs --zone=internal  #nfs
firewall-cmd --permanent --add-service=mountd --zone=internal  #nfs
firewall-cmd --permanent --add-service=rpc-bind --zone=internal  #nfs
firewall-cmd --reload

PPPoEだと速度が出ないので、普通に192.168.251.xxxとNTT光のLANの間をルーティング

enp3s0がinternalで、enp0s25がpublic(NTT側)

firewall-cmd --permanent --zone=internal --change-interface=enp3s0
firewall-cmd --permanent --zone=internal --add-masquerade
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -o enp0s25 -j MASQUERADE
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i enp3s0 -o enp0s25 -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i enp0s25 -o enp3s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
firewall-cmd --reload
firewall-cmd --direct --get-all-rules #ルールの確認
firewall-cmd --direct --get-all-rules --permanent #permanentルールの確認

ポート転送をする場合

firewall-cmd --zone=public --add-forward-port=port=1187:proto=tcp:toport=3389:toaddr=192.168.251.150 --permanent
firewall-cmd --zone=public --add-forward-port=port=1186:proto=tcp:toport=3389:toaddr=192.168.251.214 --permanent

ポート転送をやめる場合

firewall-cmd --zone=public --remove-forward-port=port=1186:proto=tcp:toport=3389:toaddr=192.168.251.214 --permanent

Ubuntu 20での話。 ufwを有効にした後

sudo ufw allow in from 192.168.0.0/16

で研究室内からのみアクセスを受け入れる。

ルーター化するには

/etc/default/ufw

DEFAULT_FORWARD_POLICY="ACCEPT"

/etc/ufw/sysctl.conf

net.ipv4.ip_forward=1

/etc/ufw/before.rules

# *filterの前あたりに下記を追加

*nat
#-Fがあるとufw reloadしたときに古い設定をリセットしてくれる
-F
:POSTROUTING ACCEPT [0:0]

# Forward traffic from eth0 through eth1.
-A POSTROUTING -s 192.168.253.0/24 -o enp5s0 -j MASQUERADE #enp5s0がグローバル側
-A PREROUTING -i enp5s0 -p tcp -d 133.11.144.11 --dport 80 -j DNAT --to-destination 192.168.253.163:80
-A PREROUTING -i enp5s0 -p tcp -d 133.11.144.11 --dport 443 -j DNAT --to-destination 192.168.253.163:443

COMMIT
sudo ufw disable && sudo ufw enable
#before.rulesを書き換えたならば
sudo systemctl restart ufw.service

#転送ルール確認
sudo iptables -t nat -nL
sudo ufw status
sudo ufw allow 80
sudo ufw allow 443
sudo nmtui

下記の箇所を変更

│ 送信先/プレフィックス  次のホップ             メトリック        │      │
│ 192.168.0.0/16_______  192.168.253.1________  100_______ <削除> │      │
│ <追加...>                                                       │      │
│                                                                 │      │
│                                                 <取り消し> <OK> │      │
[X] このネットワークはデフォルトのルートには使用しない                      │
[X] 自動的に取得されたルートを無視                        
apt install squid apache2-utils

/etc/squid/squid.confを編集して、##auth_param basic program...が書かれているあたりに書きを追加。

auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/.htpasswd
auth_param basic children 50
auth_param basic realm Squid Basic Authentication
auth_param basic credentialsttl 1000 hours
acl password proxy_auth REQUIRED
http_access allow password
htpasswd -c /etc/squid/.htpasswd suikou

でパスワードを作成。

systemctl restart squid
ufw allow 3128

クライアントは次のような感じで設定する。

まずは、firefoxでhttp://192.168.11.1を開いて、admin:31500193でログインし、ポート転送設定を追加する。

sudo nano /etc/ufw/before.rules

で下記を追加する。

-A PREROUTING -i enp10s0 -p tcp --dport 2208 -j DNAT --to-destination 192.168.251.140:22

その後、

sudo ufw reload
  • linuxルータ構築.1622172813.txt.gz
  • 最終更新: 2021/05/28 03:33
  • by 220.110.251.40