2025年4月21日 星期一

ubuntu如何設固定IP

在 Ubuntu 上設定固定 IP(Static IP)可以依照你的 Ubuntu 版本和使用的網路管理方式略有不同。以下是針對較新版本(Ubuntu 18.04 起)使用 Netplan 的設定方式,也補充舊版使用 interfaces 的方式。


Ubuntu 18.04 / 20.04 / 22.04(使用 Netplan)

找出你的網路介面名稱:
ip link show

通常會是像 enp3s0, ens33, eth0 等。

編輯 Netplan 設定檔:
sudo nano /etc/netplan/01-netcfg.yaml

檔名可能不同,像 01-network-manager-all.yaml,你可以用 ls /etc/netplan/ 來看。

範例設定:
network:
version: 2 renderer: networkd ethernets: enp3s0: dhcp4: no addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]

請把 enp3s0 換成你實際的介面名稱,IP 改成你要用的固定 IP。

套用設定:
sudo netplan apply

舊版 Ubuntu(使用 /etc/network/interfaces

如果你用的是 Ubuntu Server 16.04 或更舊版本,可以這樣:

編輯設定檔:
sudo nano /etc/network/interfaces
加入或修改內容:
auto enp3s0
iface enp3s0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4
重新啟動網路服務:
sudo systemctl restart networking

檢查設定是否成功

ip a

ifconfig

確認 IP 是否是你設定的那個。


如果要改成dhcp就把iface enp3s0 inet static改成iface enp3s0 inet dhcp,下面的全刪掉。

沒有留言:

張貼留言