1. 程式人生 > >用ubuntu 系統做路由 ipv4+dhcp ipv6+radvd

用ubuntu 系統做路由 ipv4+dhcp ipv6+radvd

前言

裝了臺pc,插了個多口的pcie網絡卡準備當作伺服器用。其中一個lan口連線外網(isp提供ipv6),另外的口用作其他pc、網路裝置的連入。

 enp24s0:
enp28s0f1: flags=4675<UP,BROADCAST,RUNNING,ALLMULTI,MULTICAST>  mtu 1500
        inet 172.16.29.18  netmask 255.255.254.0  broadcast 172.16.29.255
        inet6 2001:da8:d805:a210:xxx  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::bbd3:ac0f:5494:a360  prefixlen 64  scopeid 0x20<link>
        inet6 2001:da8:d805:a210:xxx  prefixlen 64  scopeid 0x0<global>
        ether d4:85:64:56:0b:c6  txqueuelen 1000  (乙太網)
        RX packets 134061  bytes 82264879 (82.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 76373  bytes 12021743 (12.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

即enp28s0f1對應外網連線,enp24s0對應內網。

ip設定

因為裝的是ubuntu 桌面版 要先將network-manager 關閉,否則可能/etc/network/下的設定不生效。

systemctl stop network-manager.service

給enp24s0分配ip,

$vim /etc/network/interface

編輯如下

auto lo
iface lo inet loopback

auto enp24s0
iface enp24s0 inet static
address 192.168.100.1
netmask 255.255.255.0
broadcast 192.168.100.255

auto enp24s0
iface enp24s0 inet6 static
address 2001:da8:d805:a210:a1::1
netmask 64

安裝dhcp,

$sudo apt install isc-dhcp-server

修改

$ sudo vim /etc/default/isc-dhcp-server

INTERFACESv4="enp24s0"
INTERFACESv6=""

配置 /etc/dhcp/dhcpd.conf 檔案:

option domain-name "example.org";

default-lease-time 600;
max-lease-time 7200;


ddns-update-style none;

subnet 192.168.100.0 netmask 255.255.255.0 {
        range 192.168.100.188 192.168.100.230;
        option routers 192.168.100.1;
        option subnet-mask 255.255.255.0;
        option broadcast-address 192.168.100.255;
        option domain-name-servers 114.114.114.114;
        option ntp-servers 192.168.100.1;
        option netbios-name-servers 192.168.100.1;
}

重啟 服務

$ sudo service isc-dhcp-server restart

ipv4 轉發

編輯 /etc/sysctl.conf, 取消net.ipv4.ip_forward=1的註釋,這裡net.ipv6.conf.all.forwarding=1也一併開啟

$sysctl -p

使設定生效
使用iptables轉發

$sudo iptables -t nat -A POSTROUTING -o enp28s0f1 -j MASQUERADE
$sudo iptables -A FORWARD -i enp28s0f1 -o enp24s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$sudo iptables -A FORWARD -i enp24s0 -o enp28s0f1 -j ACCEPT

iptables 規則重啟會失效
重啟生效 參考:

儲存規則:#iptables-save >/etc/iptables-script
恢復規則:#iptables-restore>/etc/iptables-script
開機自動恢復規則,把恢復命令新增到啟動指令碼:echo ‘/sbin/iptables-restore /etc/iptables-script’ >>/etc/rc.d/rc.local

至此另外一臺pc連線enp24s0 lan口自動獲取ip,已經可以上網了.

ipv6

安裝radvd

$ apt install radvd

這裡radvd的配置檔案需要新建

$ vim /etc/radvd.conf

內容

interface enp24s0 {
        AdvSendAdvert on;
        MinRtrAdvInterval 3;
        MaxRtrAdvInterval 10;
        prefix 2001:da8:d805:a210:a1::/64{
        };
};

重啟radvd

$/etc/init.d/radvd restart