最初のインターフェイスから2番目のインターフェイスに接続されているデバイスとインターネットを共有するにはどうすればよいですか?

最初のインターフェイスから2番目のインターフェイスに接続されているデバイスとインターネットを共有するにはどうすればよいですか?

2番目のイーサネットインターフェイスに接続されているローカルネットワーク上のデバイスが最初のイーサネットインターフェイスで利用可能なインターネットを使用できるように、ネットワークをどのように設定しますか?

iproute2を使用すると、ローカルネットワーク上のデバイスとLinux PCの間の接続のみを作成でき、Linux PCにはまだインターネット接続があります。ただし、ローカルネットワーク上のデバイスはこのインターネット接続を使用できません。

[編集 2] 現在の構成は以下に基づいています。このガイド

私のIPルーティングアドレスが間違っているようですが、それは問題だと思います。

設定は次のとおりです。

Internet
   |
   |
   |
(enp0s31f6) = Linux PC = (enx00249b233bda)
                                 |
                                 |
                                 |
                           NetworkSwitch
                                 |
                                 |
                                 |---(eth0) = Raspberry Pi 1
                                 |
                                 |
                                 |---(eth0) = Raspberry Pi 2

--     ethernet cable
|      ethernet cable
(eth0) network interface name

[編集] 目標は、Linux PCとすべてのRespberry Piをインターネットに接続し、互いに接続することです。

すべてのデバイスに静的IPアドレスがあります。

Ubuntu 16.04を実行しているLinux PC

以下にリストされていないすべての設定はデフォルト設定でなければなりません。

Linux PCの現在の設定

ifconfig

enp0s31f6 Link encap:Ethernet  HWaddr 48:4d:7e:b1:94:4d  
          inet addr:128.40.57.144  Bcast:128.40.57.255  Mask:255.255.255.0
          inet6 addr: fe80::4a4d:7eff:feb1:944d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1806664 errors:0 dropped:82518 overruns:0 frame:0
          TX packets:81807 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:601022858 (601.0 MB)  TX bytes:15652101 (15.6 MB)
          Interrupt:19 Memory:f7100000-f7120000 

enx00249b233bda Link encap:Ethernet  HWaddr 00:24:9b:23:3b:da  
          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::224:9bff:fe23:3bda/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:300302 errors:0 dropped:0 overruns:0 frame:0
          TX packets:373077 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:26170910 (26.1 MB)  TX bytes:476407809 (476.4 MB)

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:193 errors:0 dropped:0 overruns:0 frame:0
          TX packets:193 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17086 (17.0 KB)  TX bytes:17086 (17.0 KB)

/etc/network/interfaces

# Static IP for internet connection
auto lo
iface lo inet loopback
auto enp0s31f6
iface enp0s31f6 inet static
address 128.40.57.144
netmask 255.255.255.0
gateway 128.40.50.245
dns-nameservers 144.82.250.1 193.160.250.1

# Network adapter interfacing with RPis
allow-hotplug enx00249b233bda
iface enx00249b233bda inet static
address 192.168.0.10
netmask 255.255.255.0
gateway 192.168.0.11
dns-nameservers 144.82.250.1 193.160.250.1
post-up ip route add 192.168.0.0/24 dev enx00249b233bda src 192.168.0.10 table rt2
post-up ip route add default via 192.168.0.11 dev enx00249b233bda table rt2
post-up ip rule add from 192.168.0.10/32 table rt2
post-up ip rule add to 192.168.0.10/32 table rt2

/etc/iproute2/rt_tables

#
# reserved values
#
255 local
254 main
253 default
0   unspec
#
# local
#
#1  inr.ruhep
1 rt2

ip route show

default via 128.40.50.245 dev enp0s31f6 onlink 
128.40.57.0/24 dev enp0s31f6  proto kernel  scope link  src 128.40.57.144 
169.254.0.0/16 dev enp0s31f6  scope link  metric 1000 
192.168.0.0/24 dev enx00249b233bda  proto kernel  scope link  src 192.168.0.10 

ラズベリーパイ1現在の設定

ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.22  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::3fa1:761c:f861:dae3  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:2f:11:38  txqueuelen 1000  (Ethernet)
        RX packets 7489  bytes 537762 (525.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7417  bytes 2128900 (2.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2270  bytes 215650 (210.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2270  bytes 215650 (210.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether dc:a6:32:2f:11:3b  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

/etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

/etc/dhcpcd.conf

# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0

# Static IP for connection to Recording PC
interface eth0
static ip_address=192.168.0.22/24
static routers=192.168.0.11
static domain_name_servers=192.168.0.11

ベストアンサー1

RasPiは同じネットワークセグメントにあり、IPアドレスは192.168.0.10であるため、現在Linux PCと通信できます。ただし、RasPiがインターネット上の何かにアクセスしようとすると、追加のルーティングのために192.168.0.11にパケットを送信しようとします。しかし、RasPiネットワーク側のLinux PCのアドレスは192.168.0.10なので、11号、Linux PCはRasPiの発信パケットを決して受信しないため、ルーティングできません。

これは間違っています。 RasPiのルーター/ゲートウェイアドレスは192.168.0に設定する必要があります。.10、.11ではありません。

gateway 192.168.0.11Linux PCの構成で指定するときに、Linux PCが自分のアドレスをenx00249b233bda宣言する必要があるわけではありません。.11その他.11アドレスがインターネットに接続されているRasPiネットワークのシステム。

これは間違っています。 Linux PCにはgateway設定ラインは必要ありません。enx00249b233bdaはいRasPi ネットワークのゲートウェイです。gatewayインターフェイス設定からこの行を削除またはコメントアウトする必要がありますenx00249b233bda

なぜこれが必要なのかわかりませんip route add。ネットワークインターフェイスを設定するだけで、192.168.0.0/24ネットワークへのルートが自動的に生成され、これはあなたのニーズに十分です。すべてのコマンドをコメントアウトして再起動し、ip route add読み続けます。

パブリックIPアドレスは1つだけなので、Linux PCでIPマスカレーディングを設定する必要があります。簡単に言えば、iptables次のようになります。

iptables -w -t nat -A POSTROUTING -s 192.168.0.0/24 -o enp0s31f6 -j MASQUERADE

次に、RasPiネットワークから外部世界へのIP転送を有効にし、返された応答パケットを受け入れるには、いくつかの非常に基本的な規則が必要です。

iptables -w -t filter -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -w -t filter -A FORWARD -i enx00249b233bda -j ACCEPT

特別な接続追跡ヘルパーを必要とするいくつかのプロトコルでは、次の行を追加する必要があります(誰かがそれを悪用する方法を見つけるまで自動的に行われました。 . 文句を言います。

iptables -w -t raw -A PREROUTING -i enx00249b233bda -p tcp --dport 21 -j CT --helper ftp

これにより、RasPiネットワークからインターネットへのFTP制御接続に必要な特別な処理が有効になります。特別な処理はFTP制御接続を受信し、そのデータ接続が自動的に通過することを可能にします。同様の処理が必要な他のシナリオもあります。

FTPに加えて、特別な処理が必要な他のプロトコルは次のとおりです。

  • SNMP(UDPポート161、アシスタント名snmp
  • SIP(TCPおよびUDP、ポート5060、アシスタント名sip
  • IRCチャット(TCP、ポート番号は異なる場合があり、アシスタント名irc

(Ubuntuにこの機能があることを知っていますが、ufwそれを使用して同等のファイアウォールルールを設定する方法がわかりません。他の人が知っている場合はここで自由に編集してください。)

上記のすべてのアクションは完全には効果がありません。有効になるまでIPv4ルーティングマスタースイッチ。まず、/etc/sysctl.confファイルに次の行が含まれていることを確認してください。

net.ipv4.ip_forward=1

その後、再起動するか、次のコマンドを実行するとすぐに設定が適用されます。

sudo sysctl -p

(このマスタースイッチが存在するのはなぜですか?あります。)

おすすめ記事