| | 1 | = Linux 主機作Gateway功能 = |
| | 2 | |
| | 3 | 用一台可以連外的主機當Gateway,其他的電腦則用這台主機建構的DHCP來連到網路,已經是很多人家裏的網路配置方法。之前大部分都用現成的filmware 來作,或用有圖形介面的工具來達成,不過這次分享的是希望完全用指令來實做。 |
| | 4 | |
| | 5 | 動機是部屬好的諸台主機已經是drbl環境了,因此drbl server有兩張網卡並對外,而要當client的只連接到內部區網中。雖然drbl client是不需要內建os的,不過考慮到有朝一日還是會用到內建的硬碟開機,所以還是把他的硬碟灌一灌。不過麻煩的是client的網路都已經接在內部網域了,在不重改接線配置的情況下,最簡單的方法還是讓drbl server這台當gateway,client透過server連到外部網路把軟體裝好在硬碟之後,再用drbl server用clone zilla功能把裝好的client 備援起來。由於是最簡單的設定,因此也不搞dhcp了,直接用static ip 定址,搭配NAT的ip_forward,讓內部的機器可以把封包送出去。 |
| | 6 | |
| | 7 | == 環境 == |
| | 8 | |
| | 9 | * drbl server (兩張網卡,eth1對內,eth2對外) |
| | 10 | |
| | 11 | || eth1 || 192.168.1.254 || 內部區網的gateway address|| |
| | 12 | || eth2 || 140.110.141.130 || 對外連接的網卡設定|| |
| | 13 | |
| | 14 | * drbl client |
| | 15 | || eth1 || 192.168.1.12 || 固定位址 || |
| | 16 | |
| | 17 | == server 設定 == |
| | 18 | === 網卡設定 === |
| | 19 | * vim /etc/network/interface |
| | 20 | {{{ |
| | 21 | auto lo |
| | 22 | iface lo inet loopback |
| | 23 | |
| | 24 | auto eth2 |
| | 25 | auto eth1 |
| | 26 | |
| | 27 | iface eth2 inet static |
| | 28 | address 140.110.141.130 |
| | 29 | netmask 255.255.255.0 |
| | 30 | network 140.110.141.0 |
| | 31 | broadcast 140.110.141.255 |
| | 32 | gateway 140.110.141.254 |
| | 33 | dns-nameservers 140.110.16.1 |
| | 34 | dns-search nchc.org.tw |
| | 35 | |
| | 36 | iface eth1 inet static |
| | 37 | address 192.168.1.254 |
| | 38 | netmask 255.255.255.0 |
| | 39 | }}} |
| | 40 | |
| | 41 | 重新啟動網路: |
| | 42 | {{{ |
| | 43 | $ /etc/init.d/network restart |
| | 44 | }}} |
| | 45 | |
| | 46 | === DNS === |
| | 47 | |
| | 48 | 安裝bind就可以解析域名了(iptables是之後有用到順便裝) |
| | 49 | {{{ |
| | 50 | $ apt-get install bind iptables |
| | 51 | }}} |
| | 52 | |
| | 53 | === NAT === |
| | 54 | 打開ip forward,並讓防火牆開啟NAT功能 |
| | 55 | {{{ |
| | 56 | $ echo 1 > /proc/sys/net/ipv4/ip_forward |
| | 57 | $ iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE |
| | 58 | }}} |
| | 59 | |
| | 60 | ==== 重開機也有效的NAT設定 ==== |
| | 61 | 以上的指令只是暫時有效,但一重開機後又回覆預設值,因此修改ip forward預設值才是長久之計: |
| | 62 | |
| | 63 | * vim /etc/sysctl.conf |
| | 64 | {{{ |
| | 65 | net.ipv4.conf.default.forwarding=1 # 把註解拿掉 |
| | 66 | }}} |
| | 67 | |
| | 68 | * vim /etc/network/if-up.d/iptables |
| | 69 | {{{ |
| | 70 | #!/bin/sh |
| | 71 | # Set up firewall rules. |
| | 72 | /sbin/iptables-restore /etc/network/iptables.rules |
| | 73 | }}} |
| | 74 | |
| | 75 | * make the script executable. |
| | 76 | {{{ |
| | 77 | # chmod 755 /etc/network/if-up.d/iptables |
| | 78 | # iptables-save > /etc/network/iptables.rules |
| | 79 | }}} |
| | 80 | |
| | 81 | ==Client設定 == |
| | 82 | |
| | 83 | Client的設定很簡單,只要設定正確的區域網路位址,gateway設定到server的對內網卡ip就可以了,連dns都不用設。 |
| | 84 | |
| | 85 | * vim /etc/network/interface |
| | 86 | {{{ |
| | 87 | auto lo |
| | 88 | iface lo inet loopback |
| | 89 | |
| | 90 | auto eth1 |
| | 91 | |
| | 92 | iface eth1 inet static |
| | 93 | address 192.168.1.12 |
| | 94 | netmask 255.255.255.0 |
| | 95 | gateway 192.168.1.254 |
| | 96 | }}} |
| | 97 | |
| | 98 | |
| | 99 | 如果server有用dhcp的話,設定更簡單了: |
| | 100 | |
| | 101 | {{{ |
| | 102 | auto lo |
| | 103 | iface lo inet loopback |
| | 104 | |
| | 105 | auto eth1 |
| | 106 | iface eth1 inet dhcp |
| | 107 | }}} |