บทความนี้จะอธิบายถึงวิธีการกำหนดค่่า Static IP บน Ubuntu โดยใช้ command line
Network Configuration บน Ubuntu 17.10 และ Ubuntu 18.04 LTS
การกำหนดค่าในส่วนของ Network บน version 17.10 ขึ้นไปจะใช้ tool ตัวใหม่ที่ชื่อว่า Netplan ซึ่งไฟล์ network configuration "/etc/network/interfaces จะไม่ถูกใช้งาน
การกำหนดค่า Static IP address ด้วย Netplan บน Ubuntu
ต่อไปนี้จะกล่าวถึงขั้นตอนในการกำหนดค่่า Static IP address ด้วย Netplan
ไฟล์ Netplan configuration จัดเก็บอยู่ที่ไดเร็กทอรี่ /etc/netplan โดยที่ไฟล์ default คือ /etc/netplan/01-netcfg.yaml
แก้ไขไฟล์ network configuration ด้วย editor (nano, vi)
$ sudo vi /etc/netplan/01-netcfg.yaml
ไฟล์ configuration yaml เป็นภาษา Python
ตัวอย่างเช่น ต้องการกำหนดค่า static IPv4 address 192.168.1.100 บน network interface ens33 และ gateway 192.168.1.254 โดยที่ใช้ free Google DNS servers 8.8.8.8 และ 8.8.4.4 สำหรับ resolve name
# This file describes the network interfaces available on your system
# For more information, see netplan(5)
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.254
nameservers:
addresses[8.8.8.8,8.8.4.4]
หลังจากแก้ไขไฟล์เรียบร้อย ต้องทำคำสั่งด้านล่างนี้เพื่อให้ค่าที่แก้ไขถูกนำมาใช้งาน
$ sudo netplan apply
หรือจะทำคำสั่งพร้อมทกับ debug ก็สามารถทำได้ด้วยคำสั่ง
$ sudo netplan --debug apply
การกำหนดค่า DHCP address ด้วย Netplan
การกำหนดค่่าสำหรับ IPv4 และ IPv6 จาก DHCP server
# This file describes the network interfaces available on your system
# For more information, see netplan(5)
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: yes
dhcp6: yes
หลังจากแก้ไขไฟล์เรียบร้อย ต้องทำคำสั่งด้านล่างนี้เพื่อให้ค่าที่แก้ไขถูกนำมาใช้งาน
$ sudo netplan apply
Network Configuration บน Ubuntu 12.04 - 17.04 (รวม Ubuntu 16.04 LTS)
ขั้นตอนที่ 1: กำหนดค่าให้กับ network interface
แก้ไขไฟล์ network configuration ด้วย editor (nano, vi)
$ sudo vi /etc/network/interfaces
หลังจากเปิดไฟล์ จะได้ข้อมูลดังด้านล่างสำหรับ version เก่า:
auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic
ระบบ Ubuntu ที่ใช้ systemd (Ubuntu 16.04 และที่ใหม่กว่า) network interface ชื่อ ens33 แทน eth0 และคำว่า 'dynamic' จะถูกแทนด้วย 'dhcp'
configuration เมื่อใช้ dhcp:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet dhcp
การกำหนดค่า static ip address สำหรับ version เก่า:
auto lo eth0
iface lo inet loopback
iface eth0 inet static
address xxx.xxx.xxx.xxx (ใส่ ip ที่นี่)
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx (ใส่ gateway ip ที่นี่, ปกติเป็น address ของ router)
ตัวอย่างของ version เก่า เช่น:
auto lo eth0
iface lo inet loopback
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
และตัวอย่างสำหรับ Ubuntu 16.04 และที่ใหม่กว่า:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
dns-nameservers 8.8.8.8 8.8.4.4
ขั้นตอนที่ 2: กำหนดค่า DNS servers
version เก่ากว่า Ubuntu 14.04 เปลี่ยนค่าในไฟล์ /etc/resolv.conf
สำหรับ version ใหม่ กำหนดค่าในไฟล์ /etc/network/interfaces
- Ubuntu versions < 14.04
$ sudo vi /etc/resolv.coonf
เพิ่มข้อมูล
nameserver xxx.xxx.xxx.xxx (ใส่ค่า dns server ip)
nameserver xxx.xxx.xxx.xxx (ใส่ค่า dns server ip เครื่องอื่น)
ตัวอย่างเข่น
nameserver 8.8.8.8
nameserver 8.8.4.4
- Ubuntu version 14.04 และที่ใหม่กว่า
เปิดไฟล์ /etc/network/interfaces และเพิ่มค่า dns server
$ sudo vi /etc/network/interfaces
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameservers 8.8.8.8
nameservers 8.8.8.8 และ 8.8.4.4 สร้างโดย Google ให้ใช้สำหรับ public เราสามารถนำมาใช้ในการกำหนด
ขั้นตอนที่ 3: Restart network
หลังจากทำการแก้ไขค่าต่างๆ เรียบร้อยแล้ว ให้ทำการ restart network service
สำหรับ Ubuntu < 14.04 ใช้ networking init script:
$ sudo /etc/init.d/networking restart
ผลลัพธ์จะแสดงค่าดังด้านล่าง
*Reconfiguring network interfaces... [OK]
สำหรับ Ubuntu 14.04 และ version ใหม่กว่า ใช้ systemctl แทน:
$ sudo systemctl restart ifup@eth0
จากนั้นคุณสามารถตรวจสอบดูว่าค่าที่กำหนดไว้ถูกต้องหรือไม่ด้วยคำสั่ง:
$ ifconfig
การกำหนดค่า hostname
hostname ของ Ubuntu Server หรือ Desktop จะกำหนดค่าในไฟล์ /etc/hostname และ /etc/hosts
ไฟล์ /etc/hostname จะกำหนดค่า hostname ที่แท้จริง ขณะที่ /etc/hosts จะใช้สำหรับ local name resolution
อันดับแรก แก้ไขไฟล์ /etc/hostname
$ sudo vi /etc/hostname
จากนั้นให้กำหนดเฉพาะชื่อ hostname เท่านั้น เช่น obtlix
obtlix
และบันทึกไฟล์
จากนั้นแก้ไขไฟล์ /etc/hosts
$ sudo vi /etc/hosts
และเปลี่ยนค่า IP address พร้อมชื่อ host เช่น:
192.168.1.100 obtlix.example.com obtlix
รูปแบบในการกำหนดค่าในไฟล์นี้คือ
[IP Address] [Full hostname + domain] [hostname]
จากนั้นทำการ restart เครื่องเพื่อให้ค่าที่แก้ไขถูกใช้งาน
ขอบคุณข้อมูลจากลิ้ง https://www.howtoforge.com/linux-basics-set-a-static-ip-on-ubuntu
แก้ไขไฟล์ network configuration ด้วย editor (nano, vi)
$ sudo vi /etc/netplan/01-netcfg.yaml
ไฟล์ configuration yaml เป็นภาษา Python
ตัวอย่างเช่น ต้องการกำหนดค่า static IPv4 address 192.168.1.100 บน network interface ens33 และ gateway 192.168.1.254 โดยที่ใช้ free Google DNS servers 8.8.8.8 และ 8.8.4.4 สำหรับ resolve name
# This file describes the network interfaces available on your system
# For more information, see netplan(5)
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.254
nameservers:
addresses[8.8.8.8,8.8.4.4]
หลังจากแก้ไขไฟล์เรียบร้อย ต้องทำคำสั่งด้านล่างนี้เพื่อให้ค่าที่แก้ไขถูกนำมาใช้งาน
$ sudo netplan apply
หรือจะทำคำสั่งพร้อมทกับ debug ก็สามารถทำได้ด้วยคำสั่ง
$ sudo netplan --debug apply
การกำหนดค่า DHCP address ด้วย Netplan
การกำหนดค่่าสำหรับ IPv4 และ IPv6 จาก DHCP server
# This file describes the network interfaces available on your system
# For more information, see netplan(5)
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: yes
dhcp6: yes
หลังจากแก้ไขไฟล์เรียบร้อย ต้องทำคำสั่งด้านล่างนี้เพื่อให้ค่าที่แก้ไขถูกนำมาใช้งาน
$ sudo netplan apply
Network Configuration บน Ubuntu 12.04 - 17.04 (รวม Ubuntu 16.04 LTS)
ขั้นตอนที่ 1: กำหนดค่าให้กับ network interface
แก้ไขไฟล์ network configuration ด้วย editor (nano, vi)
$ sudo vi /etc/network/interfaces
หลังจากเปิดไฟล์ จะได้ข้อมูลดังด้านล่างสำหรับ version เก่า:
auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic
ระบบ Ubuntu ที่ใช้ systemd (Ubuntu 16.04 และที่ใหม่กว่า) network interface ชื่อ ens33 แทน eth0 และคำว่า 'dynamic' จะถูกแทนด้วย 'dhcp'
configuration เมื่อใช้ dhcp:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet dhcp
การกำหนดค่า static ip address สำหรับ version เก่า:
auto lo eth0
iface lo inet loopback
iface eth0 inet static
address xxx.xxx.xxx.xxx (ใส่ ip ที่นี่)
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx (ใส่ gateway ip ที่นี่, ปกติเป็น address ของ router)
ตัวอย่างของ version เก่า เช่น:
auto lo eth0
iface lo inet loopback
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
และตัวอย่างสำหรับ Ubuntu 16.04 และที่ใหม่กว่า:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
dns-nameservers 8.8.8.8 8.8.4.4
ขั้นตอนที่ 2: กำหนดค่า DNS servers
version เก่ากว่า Ubuntu 14.04 เปลี่ยนค่าในไฟล์ /etc/resolv.conf
สำหรับ version ใหม่ กำหนดค่าในไฟล์ /etc/network/interfaces
- Ubuntu versions < 14.04
$ sudo vi /etc/resolv.coonf
เพิ่มข้อมูล
nameserver xxx.xxx.xxx.xxx (ใส่ค่า dns server ip)
nameserver xxx.xxx.xxx.xxx (ใส่ค่า dns server ip เครื่องอื่น)
ตัวอย่างเข่น
nameserver 8.8.8.8
nameserver 8.8.4.4
- Ubuntu version 14.04 และที่ใหม่กว่า
เปิดไฟล์ /etc/network/interfaces และเพิ่มค่า dns server
$ sudo vi /etc/network/interfaces
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameservers 8.8.8.8
nameservers 8.8.8.8 และ 8.8.4.4 สร้างโดย Google ให้ใช้สำหรับ public เราสามารถนำมาใช้ในการกำหนด
ขั้นตอนที่ 3: Restart network
หลังจากทำการแก้ไขค่าต่างๆ เรียบร้อยแล้ว ให้ทำการ restart network service
สำหรับ Ubuntu < 14.04 ใช้ networking init script:
$ sudo /etc/init.d/networking restart
ผลลัพธ์จะแสดงค่าดังด้านล่าง
*Reconfiguring network interfaces... [OK]
สำหรับ Ubuntu 14.04 และ version ใหม่กว่า ใช้ systemctl แทน:
$ sudo systemctl restart ifup@eth0
จากนั้นคุณสามารถตรวจสอบดูว่าค่าที่กำหนดไว้ถูกต้องหรือไม่ด้วยคำสั่ง:
$ ifconfig
การกำหนดค่า hostname
hostname ของ Ubuntu Server หรือ Desktop จะกำหนดค่าในไฟล์ /etc/hostname และ /etc/hosts
ไฟล์ /etc/hostname จะกำหนดค่า hostname ที่แท้จริง ขณะที่ /etc/hosts จะใช้สำหรับ local name resolution
อันดับแรก แก้ไขไฟล์ /etc/hostname
$ sudo vi /etc/hostname
จากนั้นให้กำหนดเฉพาะชื่อ hostname เท่านั้น เช่น obtlix
obtlix
และบันทึกไฟล์
จากนั้นแก้ไขไฟล์ /etc/hosts
$ sudo vi /etc/hosts
และเปลี่ยนค่า IP address พร้อมชื่อ host เช่น:
192.168.1.100 obtlix.example.com obtlix
รูปแบบในการกำหนดค่าในไฟล์นี้คือ
[IP Address] [Full hostname + domain] [hostname]
จากนั้นทำการ restart เครื่องเพื่อให้ค่าที่แก้ไขถูกใช้งาน
ขอบคุณข้อมูลจากลิ้ง https://www.howtoforge.com/linux-basics-set-a-static-ip-on-ubuntu
การกำหนดค่า Static IP บน Ubuntu
Reviewed by Wisoot Amatariyakul
on
1:46 AM
Rating:
No comments: