--- title: DDNS Setup tags: mynote --- # DDNS setup contributed by <[`yanjiun`](https://github.com/yanjiunhaha)> ## 預先準備 DDNS 是由客戶端主動去更新 DNS Server 上的設定檔,使得自己的浮動 IP 隨時保持在最新。 因此需先完成 DNS Server 的架設,請參考 [DNS Server](https://hackmd.io/@yanjiun/ry4iCJ4HS)。 ## 開始設定 ### 在 DNS Server 端建立公/私鑰 ```bash $ cd /etc/named $ dnssec-keygen -a HMAC-MD5 -b 512 -n HOST Jacobian $ ls -l -rw-------. 1 root root 117 10月 18 07:58 Kjacobian.+157+11670.key -rw-------. 1 root root 229 10月 18 07:58 Kjacobian.+157+11670.private ``` * `-a`: 加密方式為`HMAC-MD5` * `-b`: 加密長度為`512`位元 * `-n`: 類型為`HOST` * `jacobian` 為客戶端電腦名字 * `.key` 為公鑰,`.private` 為私鑰 ### 設定 DNS Server 端 編輯 DNS 設定檔 ```bash $ vim /etc/named.conf ... key "Jacobian" { algorithm hmac-md5; secret "Gddy...EWA==" }; zone "nclab.nkust.edu.tw" IN { ... update-policy { grant Jacobian name Jacobian.nclab.nkust.edu.tw A; }; }; ... $ systemctl restart named ``` * 加上 `key` 的設定 * algorithm : 所使用的加密方式 * secret : 貼上**公鑰** * 在所需 `zone` 中加入權限設定 * `grant` : Key 的名字 * `name` : 完整的 domain name * `A` : 類型(A為指定ip) * 完成後重讀 DNS 服務設定檔,並確定無錯誤`$ systemctl status named` ### 設定客戶端 我將設定檔放在 `/usr/local/ddns` 下,將 Server 端的**公鑰與私鑰**都傳送過來,並撰寫 Script 自動向DNS Server 更新IP,`ddns_update.sh` 如下 ```bash #!/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH # 0.keyin your parameters basedir="/usr/local/ddns" keyfile="$basedir"/"Kjacobian.+157+11670.key" ttl=600 hostname="Jacobian.nclab.nkust.edu.tw" servername="dns.nclab.nkust.edu.tw" #Get your new IP newip=`ip addr | grep 'dynamic' | awk '{print $2}' | sed -e 's/\/24//'` checkip=`echo $newip | grep '^[0-9]'` if [ "$checkip" == "" ]; then echo "$0: Can't not find dynamic ip..." exit 1 fi #Create the temporal file tmpfile=/tmp/ddns_tmp.txt cd $basedir echo "server $servername" > $tmpfile echo "update delete $hostname A" >> $tmpfile echo "update add $hostname $ttl A $newip" >> $tmpfile echo "send" >> $tmpfile #Send your dyncmic ip to dns server nsupdate -k $keyfile -v $tmpfile ``` * 要設為可執行,並測試沒問題 ### 使用 Cron 定時執行 Script 設定每個小時自動執行 ```bash $ cd /etc/cron.hourly $ ln -s /usr/local/ddns/ddns_update.sh ddns_update.sh ``` ## References 1. [鳥哥的Linux私房菜-DNS](http://linux.vbird.org/linux_server/0350dns.php#ddns)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up