---
# System prepended metadata

title: Linux Router 筆記

---

# Linux Router 筆記


## Topology

![topology](https://i.imgur.com/XZvoGlt.jpg)

## Conf

### Ubuntu 16.04

* 把這台主機設定為**Host-Only**
* 設定預設路由指向Linux Router
```
$ sudo route add default gw 10.0.0.129
```
![](https://i.imgur.com/FtjV7jr.jpg)



### Linux Router

* 當Router的這台須有**兩張網卡**，一張設定為**Bridge**一張設定為**Host-Only**

* 然後必須打開**路由轉發**功能

```
$ sudo vim /proc/sys/net/ipv4/ip_forward  // 改為 1
$ sudo vim /etc/sysctl.conf 
    net.ipv4.ip_forward = 1
$ sudo sysctl -p
```

## Lab 1

* 上述設定無誤後，**My Host**也得設定一條路由到**Linux Router**

```
$ sudo route add default gw 192.168.1.100
```

* **Ubuntu16.04**這台就可以跟外面的**My Host**連通
![lab1](https://i.imgur.com/dPczhon.jpg)

## Lab 2

* 此時**Ubuntu16.04**這台是無法連上Internet的，還須設定NAT才行

`$ sudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j SNAT --to 192.168.1.100`

* **Ubuntu16.04**這台就可以正常上網
![](https://i.imgur.com/RGqW0td.jpg)




## Reference 
* https://www.cnblogs.com/jjzd/p/6503020.html
* https://blog.51cto.com/lustlost/943110