How To Set Static IP Address : Raspberry Pi

หากเราต้องการ fixed ip ให้กับเจ้า Pi มันก็มีวิธีที่ง่ายๆ โดยทำผ่าน ssh ด้วยคำสั่งนี้

sudo nano /etc/dhcpcd.conf

คัดลอกไปวางไว้บรรทัดสุดท้ายเลย หากต้องการตั้งค่าให้ LAN ก็ใช้ eth0 แต่ถ้าตั้งให้ WIFI ก็ตั้ง wlan0 กำหนด ip, gateway, ns ได้ตามใจชอบเลย

interface eth0
static ip_address=192.168.0.12/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8 8.8.4.4
interface wlan0
static ip_address=192.168.1.13/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8 8.8.4.4

เมื่อมั่นใจว่าทุกอย่างถูกต้อง กด Ctrl + x ตามด้วย Y และกด ENTER

จากนั้น reboot เครื่องแล้วลอง ping ip ที่ตั้งไปใหม่ดูว่า ใช้งานได้หรือไม่ ตามตัวอย่าง

pi@raspberrypi:~ $ sudo nano /etc/dhcpcd.conf
pi@raspberrypi:~ $ sudo reboot
Connection to 129.0.3.42 closed by remote host.
Connection to 129.0.3.42 closed.
PS C:\Users\MoreMeng> ping 129.0.14.4

Pinging 129.0.14.4 with 32 bytes of data:
Reply from 129.0.14.4: bytes=32 time=1ms TTL=63
Reply from 129.0.14.4: bytes=32 time<1ms TTL=64
Reply from 129.0.14.4: bytes=32 time<1ms TTL=64
Reply from 129.0.14.4: bytes=32 time<1ms TTL=64

Ping statistics for 129.0.14.4:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms
PS C:\Users\MoreMeng>