幫 ubuntu 16.04 換 static ip
===
Ref:
- [Ubuntu解決RTNETLINK answers: File exists_兜兜空蕩蕩的博客-CSDN博客](https://blog.csdn.net/w18mc0431/article/details/79661869)
- [Linux下常用的配置IP及route的命令匯編_李如磊的技術博客_51CTO博客](https://blog.51cto.com/lee90/1690606)
- [How to Set Static IP Address and Configure Network in Linux](https://www.tecmint.com/set-add-static-ip-address-in-linux/)
- [How to Set a Static IP on a Linux Machine | IT Support Blog](https://sites.cns.utexas.edu/oit-blog/blog/how-set-static-ip-linux-machine)
---
1. 打開這個檔案
```bash=
# vim /etc/network/interfaces
```
1. Default 只會有這個
```bash=
# The loopback network interface
auto lo
iface lo inet loopback
```
> Ubuntu desktop system 是用 [NetworkManager](https://en.wikipedia.org/wiki/NetworkManager) 去管理網路連線 (automatically and dynamically)
Ref: [networking - /etc/network/interfaces file almost empty - Server Fault](https://serverfault.com/questions/816064/etc-network-interfaces-file-almost-empty)
1. 在後面加上類似這樣的東西
```
auto eth0
iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 4.4.4.4
dns-nameservers 8.8.8.8
```
1. eth0 要換成自己的 network interface 的名字,how to check:
```bash=
ip link
```
1. Restart network
```bash=
sudo ifdown -a # 關閉所有網路
sudo ifup -a # 打開所有網路
```
1. 如果出現這種 error
```
RTNETLINK answers: File exists
Failed to bring up eth2.
```
```
ip addr flush dev eth0 # 清空 eth0 網卡的配置
ifdown eth0 && ifup eth0
```
> 如果用 ssh, `ip addr flush` 會斷線 (會一直 hang 住的感覺)
> 好像又可以回來?
Debian 也差不多
[How to set up static IP address on Debian Linux 10/11 - nixCraft](https://www.cyberciti.biz/faq/add-configure-set-up-static-ip-address-on-debianlinux/)
---
### 掃沒有在用的 ip
Ref:
- [How to Find Unused IP Addresses in Network - Fedingo](https://fedingo.com/how-to-find-unused-ip-addresses-in-network/)
- [Nmap Cheat Sheet](https://www.stationx.net/nmap-cheat-sheet/)
```bash
$ sudo nmap -sn 192.168.1.0/24 -v
```
`-sn`: Disable port scanning. Host discovery only.