카이도스의 Tech Blog

홈 네트워크 구축(VPN) - WireGuard, DDNS등 본문

Network

홈 네트워크 구축(VPN) - WireGuard, DDNS등

카이도스 2025. 3. 28. 13:25
728x90
반응형
집에서도 PROXMOX 셋팅 후 그위에서 여러 테스트를 하고있습니다.
추가로 외부에서도 집에있는 서버에 붙어서 작업을 하고싶어서 VPN 셋팅을 진행하게 되었네요.
원래는 유료로 공인IP를 받을까했지만, ddns 통해 무료로 가능한 방법이 있어서 진행하였고 해당 내용 공유드립니다.!

1. DuckDNS에서 DDNS 도메인 등록

  • https://www.duckdns.org/ 접속 후 로그인 (Google, Github 등으로)
  • 원하는 서브도메인 등록 (예: pjhvpn.duckdns.org)
  • 발급받은 토큰(Token) 확인 (이후 스크립트에 사용)
# 토큰
191c....


2. 공유기 NAT (포트포워딩) 설정

  • 공유기 접속 후, NAT → 포트포워딩 설정
  • WireGuard 포트포워딩 입력:
항목
이름 VPN
프로토콜 UDP
외부 포트 시작 51820
외부 포트 끝 51820
내부 IP 192.....
내부 포트 51820

➡️ 등록 완료 후 외부에서 51820/UDP가 VPN 서버로 포워딩됨.


3. 유동 IP 자동 갱신 스크립트 작성 (vpn 서버)

# 디렉터리 생성 및 파일 생성
mkdir -p ~/duckdns && cd ~/duckdns
cat <<EOT >> duck.sh
#!/bin/bash
curl -k -o ~/duckdns/duck.log "https://www.duckdns.org/update?domains=pjhvpn&token=....&ip="
EOT

# 실행 권한
chmod 700 duck.sh
./duck.sh

# OK 확인
cat duck.log
OK

# crontab
sudo su -
cat <<EOT >> /etc/crontab
# duck dns
*/5 * * * * ubuntu /home/ubuntu/duckdns/duck.sh >/dev/null 2>&1
EOT

# 확인
sudo systemctl status cron.service 
● cron.service - Regular background program processing daemon
     Loaded: loaded (/usr/lib/systemd/system/cron.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-03-20 04:44:45 UTC; 12s ago
       Docs: man:cron(8)
   Main PID: 906 (cron)
      Tasks: 1 (limit: 4609)
     Memory: 420.0K (peak: 672.0K)
        CPU: 2ms
     CGroup: /system.slice/cron.service
             └─906 /usr/sbin/cron -f -P

Mar 20 04:44:45 vpn systemd[1]: Started cron.service - Regular background program processing daemon.
Mar 20 04:44:45 vpn (cron)[906]: cron.service: Referenced but unset environment variable evaluates to an empty string: EXTRA_OPTS
Mar 20 04:44:45 vpn cron[906]: (CRON) INFO (pidfile fd = 3)
Mar 20 04:44:45 vpn cron[906]: (CRON) INFO (Running @reboot jobs)

grep CRON /var/log/syslog
2025-03-20T04:36:25.011608+00:00 ubuntu cron[965]: (CRON) INFO (pidfile fd = 3)
2025-03-20T04:36:25.021674+00:00 ubuntu cron[965]: (CRON) INFO (Running @reboot jobs)
2025-03-20T04:38:28.898737+00:00 vpn cron[904]: (CRON) INFO (pidfile fd = 3)
2025-03-20T04:38:28.902344+00:00 vpn cron[904]: (CRON) INFO (Running @reboot jobs)
2025-03-20T04:39:18.067126+00:00 vpn cron[904]: (CRON) INFO (pidfile fd = 3)
2025-03-20T04:39:18.070479+00:00 vpn cron[904]: (CRON) INFO (Running @reboot jobs)
2025-03-20T04:43:09.030997+00:00 vpn cron[899]: (CRON) INFO (pidfile fd = 3)
2025-03-20T04:43:09.034288+00:00 vpn cron[899]: (CRON) INFO (Running @reboot jobs)
2025-03-20T04:44:45.079327+00:00 vpn cron[906]: (CRON) INFO (pidfile fd = 3)
2025-03-20T04:44:45.082485+00:00 vpn cron[906]: (CRON) INFO (Running @reboot jobs)
2025-03-20T04:45:01.087039+00:00 vpn CRON[1058]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
2025-03-20T04:45:01.088768+00:00 vpn CRON[1060]: (ubuntu) CMD (/home/ubuntu/duckdns/duck.sh >/dev/null 2>&1)

tail -f ~/duckdns/duck.log
OK

4. WireGuard VPN 서버 설치 및 설정

# 디렉터리 구조
tree wireguard/
wireguard/
├── clients
│   └── pjh
│       ├── pjh.conf
│       ├── pjh_private.key
│       └── pjh_public.key
└── server
    ├── server_private.key
    └── server_public.key
# 포트 포워딩 설정
sudo vi /etc/sysctl.conf 
net.ipv4.ip_forward = 1

sudo sysctl -p
net.ipv4.ip_forward = 1

# WireGuard VPN 설치 및 설정
sudo apt update
sudo apt install -y wireguard

# 키생성
mkdir -p ~/wireguard/server && cd ~/wireguard/server
wg genkey | tee server_private.key | wg pubkey > server_public.key

# 확인
cat server_public.key 
p6gV....
cat server_private.key 
QEmr....

# 설정 변경
sudo vi /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24                  # 서버 wg0 인터페이스 IP
ListenPort = 51820
PrivateKey = QE....

# VPN 클라이언트의 인터넷 사용을 위한 NAT 활성화
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE

# 재시작
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
sudo systemctl status wg-quick@wg0
  • 사용자 계정 생성
# 폴더, 키생성
mkdir -p ~/wireguard/clients/pjh
cd ~/wireguard/clients/pjh
wg genkey | tee pjh_private.key | wg pubkey > pjh_public.key

# 확인
cat pjh_public.key 
4gh....
cat pjh_private.key 
mPkv....

# wg0.conf에 Peer 등록
sudo vi /etc/wireguard/wg0.conf
....
[Peer]
#pjh
PublicKey = 4gh....
AllowedIPs = 10.0.0.2/32

# 클라이언트 conf
cd ~/wireguard/clients/pjh/pjh.conf
cat <<EOT > ~/wireguard/clients/pjh/pjh.conf
[Interface]
PrivateKey = mPkv.....
Address = 10.0.0.2/24
DNS = 192.....

[Peer]
PublicKey = p6....
Endpoint = pjhvpn.duckdns.org:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOT

# 서비스 재시작 및 확인
sudo systemctl restart wg-quick@wg0
sudo systemctl status wg-quick@wg0

5. 클라이언트 설정 및 WireGuard 접속

pjh.conf 파일 가져가기

  • pjh.conf 파일을 PC나 모바일(안드로이드/iOS)로 복사
    • 메일로 보내거나
    • scp, sftp로 다운로드
    • 모바일은 QR코드로도 가능

클라이언트에 WireGuard 설치


pjh.conf Import

PC:

  1. WireGuard 클라이언트 실행
  2. Add TunnelImport from filepjh.conf 선택
  3. 활성화 버튼 ON → 바로 VPN 연결됨

모바일:

  1. WireGuard 앱 실행
  2. Add Tunnel → Import from file or QR Code
  3. 터널 ON → 바로 내부망 IP 할당됨 (10.0.0.2)

6. 확인

# 확인 명령어
sudo wg show
interface: wg0
  public key: p6g....
  private key: (hidden)
  listening port: 51820

peer: 4g....
  endpoint: 211.....:54188
  allowed ips: 10.0.0.2/32
  latest handshake: 51 minutes, 49 seconds ago
  transfer: 21.89 MiB received, 30.97 MiB sent
728x90
반응형
Comments