お問い合わせ

ブログ

これまでに経験してきたプロジェクトで気になる技術の情報を紹介していきます。

CentOS6を使ったルーティング

Ryuichirou Ryuichirou 4 years
CentOS6を使ったルーティング

CentOS6を使って外部と接続されたネットワークを踏み台となるサーバを通じてsshでアクセスするための設定。 ただし上位のルータからは踏み台となるサーバにsshのポートがフォワーディングされていることが前提。

eth0 : 192.168.80.39  (上位のGWは 192.168.80.1)
eth1 : 192.168.100.39   (上位のGWは 192.168.100.1)

①ネットワークの確認

[root@gwrt network-scripts]# ifconfig
eth0      Link encap:Ethernet  HWaddr 74:27:EA:AA:36:D6
          inet addr:192.168.80.39  Bcast:192.168.80.255  Mask:255.255.255.0
          inet6 addr: 240b:250:500:4300:7627:eaff:feaa:36d6/64 Scope:Global
          inet6 addr: fe80::7627:eaff:feaa:36d6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5904 errors:0 dropped:0 overruns:0 frame:0
          TX packets:392 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:387908 (378.8 KiB)  TX bytes:46395 (45.3 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0D:0B:48:89:07
          inet addr:192.168.100.39  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: 240b:250:500:4300:20d:bff:fe48:8907/64 Scope:Global
          inet6 addr: fe80::20d:bff:fe48:8907/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8592 errors:0 dropped:0 overruns:0 frame:0
          TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1047909 (1023.3 KiB)  TX bytes:7069 (6.9 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

②各設定ファイルの内容

・eth0(外部ネットワーク向け)
[root@gwrt munetika]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
IPADDR=192.168.80.39
#GATEWAY=192.168.80.1
PREFIX=24
NAME="System eth0"

・eth1(内部ネットワーク向け)
[root@gwrt munetika]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
#NM_CONTROLLED=yes
#BOOTPROTO=none
IPADDR=192.168.100.39
#GATEWAY=192.168.100.1
PREFIX=24
NAME="System eth1"

・ルーティングをONにする(net.ipv4.ip_forward = 0 を net.ipv4.ip_forward = 1 に)

[root@gwrt munetika]# cat /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.
#
# Use '/sbin/sysctl -a' to list all possible parameters.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

・デフォルトゲートウェイは外部ネットワークのルータに向ける

[root@gwrt munetika]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=gwrt.aska-ltd.jp
FORWARD_IPV4=yes
GATEWAYDEV=eth0
GATEWAY=192.168.80.1

・ルーティングの追加(192.168.80.0を流れてくるパケットは eth1 のゲートウェイを経由させて内部に流す)

[root@gwrt munetika]# cat /etc/sysconfig/network-scripts/route-eth1
192.168.80.0/24 via 192.168.100.1

もし、複数のルートを持たせたい場合は以下のように書く

[root@mitakaucom network-scripts]# cat /etc/sysconfig/network-scripts/route-eth1
192.168.109.0/24 via 192.168.0.254
192.168.10.0/24 via 192.168.0.254

③ルーティングの確認

[root@gwrt munetika]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.100.0   0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.80.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth1
0.0.0.0         192.168.80.1    0.0.0.0         UG        0 0          0 eth0
[root@gwrt munetika]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.100.0   *               255.255.255.0   U         0 0          0 eth1
192.168.80.0    *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth1
default         192.168.80.1    0.0.0.0         UG        0 0          0 eth0

④外部からsshでログインし、正常にポートフォワーディングされているかどうか確認する

※参考URL http://www.unix-power.net/linux/route.html

2019/6/13追記 CentOS7でも同様にルータ化できます eth0 -> enp0s16f0u1 に eth1 -> enp1s0 に読み替えます。

/etc/sysctl.conf は以下の1行のみが足されることになります net.ipv4.ip_forward = 1

/etc/sysconfig/network-scripts/route-eth1 は etc/sysconfig/network-scripts/route-enp1s0 と読み替えて内容は 192.168.80.0/24 via 192.168.100.1 のままです。

netstat -nr の結果は Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.80.1 0.0.0.0 UG 0 0 0 enp0s16f0u1 192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s16f0u1 192.168.80.0 192.168.100.1 255.255.255.0 UG 0 0 0 enp1s0 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0 となります

CentOS6を使ったルーティング 2021-08-24 12:36:32

コメントはありません。

4621

お気軽に
お問い合わせください。

お問い合わせ
gomibako@aska-ltd.jp