2019-03-13

Ubuntu 18.04 設定靜態 IP

設定 Ubuntu 18.04 靜態 IP

先要查出網卡的名稱

if config -a

查出來的結果:

docker0: flags=4099<up,broadcast,multicast>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:54:82:ea:09  txqueuelen 0  (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

eth0: flags=4163<up,broadcast,running,multicast>  mtu 1500
        inet 192.168.88.88  netmask 255.255.240.0  broadcast 192.168.95.255
        inet6 fe80::215:5dff:fe58:7316  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:58:73:16  txqueuelen 1000  (Ethernet)
        RX packets 609415  bytes 783165377 (783.1 MB)
        RX errors 0  dropped 3521  overruns 0  frame 0
        TX packets 94741  bytes 7497987 (7.4 MB)
        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 170  bytes 13177 (13.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 170  bytes 13177 (13.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在我的環境中能看到三個網路介面卡,docker0 是安裝 docker 產生的虛擬交換器,是要給docker的容器使用的。lo 是 loop back 的介面卡。這邊實際要設定的網路卡名稱是 eth0。

如果在 /etc/netplan/ 路徑中沒有設定檔,那可能你安裝系統時就沒有網路卡,所以 ubuntu 沒有自動產生。那就要自己用 sudo netplan generate 指令產生一個設定檔。

sudo netplan generate

我的環境在安裝時就已經有一個檔案50-cloud-init.yaml,所以直接編輯。原始內容如下:

cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
    version: 2

這裡我透過編輯器將內容修改如下:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: no
            addresses: [192.168.88.88/24]
            gateway4: 192.168.88.254
            nameservers:
                    addresses: [168.95.1.1, 8.8.8.8]
    version: 2
  • eth0: 網路介面卡名稱
  • dhcp4 或 dhcp6: dhcp 設定,各別是 IPv4 或 IPv6。yes|no
  • addresses: 靜態IP設定
  • gateway4: IPv4 的網路閘道
  • nameservers: 域名解析伺服器

設定完後後要將設定套用

sudo netplan apply

1 則留言:

Nobody 提到...

if config -a
if與config中間不應有空格

adsense