Archive for the ‘Debian’ Category

ติดตั้ง WireGuard บน Debian 13 (Trixie)

เช็คก่อนนะว่า ใช้ Linux Kernel Version อะไร
# ดู kernel version
uname -r

WireGuard ต้องการ 5.6+ และ module ถ้าไม่มี ต้อง upgrade kernel ก่อน
# ตรวจสอบ kernel ที่มีให้ติดตั้ง
apt search linux-image | grep amd64

# ติดตั้ง kernel ใหม่
apt install -y linux-image-amd64 linux-headers-amd64

# ตรวจสอบว่าติดตั้งสำเร็จ
dpkg -l | grep linux-image

แล้ว reboot:

หลัง reboot ตรวจสอบ kernel version ใหม่:
uname -r
# ควรได้ 6.x.x หรืออย่างน้อย 5.x.x

# ทดสอบ load module
modprobe wireguard
echo $? # ถ้าได้ 0 = สำเร็จ

ถ้า modprobe wireguard ผ่านแล้ว ค่อย:

systemctl enable –now wg-quick@wg0

Step 1: ติดตั้ง
apt update && apt install -y wireguard wireguard-tools

Step 2: สร้าง Key Pair บน Server
cd /etc/wireguard
umask 077

# Server keys
wg genkey | tee server_private.key | wg pubkey > server_public.key

# Client key (ทำซ้ำต่อ client)
wg genkey | tee client1_private.key | wg pubkey > client1_public.key

cat server_public.key # เก็บไว้ใส่ใน client config
cat client1_public.key # เก็บไว้ใส่ใน server config

ตรวจเช็คไฟล์ที่สร้าง
root@np:/etc/wireguard# ls

client1_private.key  client1_public.key  server_private.key  server_public.key

root@np:/etc/wireguard# cat server_private.key

xxxxxxxxxxx(Server_Private Key)xxxxxxxxxxxxx

root@np:/etc/wireguard# cat client1_public.key

xxxxxxxxxxx(Client Public Key)xxxxxxxxxxxxx

GNU nano 8.x สร้างไฟล์ใหม่ /etc/wireguard/wg0.conf *
root@np:/etc/wireguard# cat wg0.conf
#EXAMPLE
#[Interface]
#Address = 10.10.10.11/24
#ListenPort = 51820
#PrivateKey = <server_private.key content>

# เปิด IP Forwarding (ถ้าต้องการ route traffic)
#PostUp = sysctl -w net.ipv4.ip_forward=1
#PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
#PostDown = iptables -D FORWARD -i wg0 -j ACCEPT

# Client 1 – Laptop
#[Peer]
#PublicKey = <client1_public.key content>
#AllowedIPs = 10.10.10.111/32

# Client 2 – Phone
#[Peer]
#PublicKey = <client2_public.key content>
#AllowedIPs = 10.10.10.112/32

[Interface]
Address = 172.16.19.1/24
ListenPort = 51820
PrivateKey = xxxxxxxxxxx(Server_Private Key)xxxxxxxxxxxxx

PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT

# Client 1 – Laptop
[Peer]
PublicKey = xxxxxxxxxxx(Client Public Key)xxxxxxxxxxxxx
AllowedIPs = 172.16.19.10/32

 

ที่โปรแกรม WireGuard บน Client ให้ใส่ค่า Config นี้

[Interface]
Address = 172.16.19.10/24
PrivateKey = client1_private.key

[Peer]
PublicKey = server_public.key
Endpoint = public_ip:51820
AllowedIPs = 172.16.19.0/24
PersistentKeepalive = 25

ลองกดเชื่อมต่อ (on) จะต้องต่อไปที่ Server ได้

จะเช็คได้ยังไงว่ามี Client เชื่อมต่อเข้ามา
ดู Client ที่ connect อยู่ทั้งหมด
ตัวอย่าง output ที่ควรเห็น:

root@np:/etc/wireguard# wg show
interface: wg0
public key: xxxxxx
private key: (hidden)
listening port: 51820

peer: xxxxx
endpoint: x.x.x.x:64135 ← IP จริงของ client
allowed ips: 172.16.19.10/32
latest handshake: 2 minutes ago ← ถ้าเห็นนี้ = connected
transfer: 12.38 KiB received, 3.65 KiB sent

ถ้า ไม่เห็น latest handshake = client ยังไม่ได้ connect

เช็ค ping ไปหา client
# ping ไปที่ VPN IP ของ client
ping 172.16.19.10

root@np:/etc/wireguard# iptables -I INPUT 1 -i wg0 -j ACCEPT (เพิ่มกฏนี้ให้สามารถ ping จาก Client ไปยัง Server ได้)

ลอง Netstat -lntup ควรที่จะเห็น
root@np:/etc/wireguard# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

udp 0 0 0.0.0.0:51820 0.0.0.0:* –

สาเหตุที่ netstat แสดง – เพราะ WireGuard ใช้ kernel module โดยตรง (wireguard.ko) ไม่มี userspace daemon ที่จะโชว์ใน process list เหมือน Program อื่น ถือว่าปกติ 100%

สร้าง Key หลาย Client พร้อมกันเลย
cd /etc/wireguard

# สร้างทีเดียว 5 client
for i in 1 2 3 4 5; do
wg genkey | tee client${i}_private.key | wg pubkey > client${i}_public.key
echo “Client $i Public Key: $(cat client${i}_public.key)”
done

เพิ่ม Peer ใน wg0.conf
cat >> /etc/wireguard/wg0.conf << EOF

# Client 2 – Phone
[Peer]
PublicKey = $(cat /etc/wireguard/client2_public.key)
AllowedIPs = 172.16.19.11/32

# Client 3 – Office PC
[Peer]
PublicKey = $(cat /etc/wireguard/client3_public.key)
AllowedIPs = 172.16.19.12/32

# Client 4
[Peer]
PublicKey = $(cat /etc/wireguard/client4_public.key)
AllowedIPs = 172.16.19.13/32

# Client 5
[Peer]
PublicKey = $(cat /etc/wireguard/client5_public.key)
AllowedIPs = 172.16.19.14/32
EOF

reload โดยไม่ต้อง restart:

wg addconf wg0 <(wg-quick strip wg0)
# หรือ
systemctl reload wg-quick@wg0

ตัวอย่าง Network Interface – Debian 13.1

# 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
allow-hotplug enp0s31f6
iface enp0s31f6 inet dhcp

#auto enp0s31f6
#iface enp0s31f6 inet static
# address 192.168.100.42
# netmask 255.255.255.0
# gateway 192.168.100.1
# dns-nameservers 8.8.8.8 1.1.1.1

 

 

Notebook- ปรับการตั้งค่า power ของระบบ (ไม่ให้ suspend เมื่อพับหน้าจอ)-Debian

ปัญหาที่เจอบน Debian/ThinkPad คือเวลาพับหน้าจอ ระบบจะเข้าสู่ suspend/hibernate ทำให้ SSH server หยุดทำงาน ทำให้ไม่สามารถเข้า SSH ได้

วิธีแก้
ปรับการตั้งค่า power ของระบบ (ไม่ให้ suspend เมื่อพับหน้าจอ)
แก้ไขไฟล์ logind configuration:
sudo nano /etc/systemd/logind.conf

หา (หรือเพิ่ม) บรรทัดเหล่านี้:
HandleLidSwitch=ignore
HandleLidSwitchDocked=ignore

HandleLidSwitch=ignore → เมื่อพับฝา ไม่ทำ suspend
HandleLidSwitchDocked=ignore → ใช้เวลาเชื่อมต่อ docking station

รีสตาร์ท systemd-logind:
sudo systemctl restart systemd-logind

ทดสอบ: พับหน้าจอแล้วเครื่องยังคงทำงาน คุณยังสามารถ SSH เข้าได้