contributed by <yanjiun
>
DDNS 是由客戶端主動去更新 DNS Server 上的設定檔,使得自己的浮動 IP 隨時保持在最新。
因此需先完成 DNS Server 的架設,請參考 DNS Server。
$ 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 設定檔
$ 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
的設定
zone
中加入權限設定
grant
: Key 的名字name
: 完整的 domain nameA
: 類型(A為指定ip)$ systemctl status named
我將設定檔放在 /usr/local/ddns
下,將 Server 端的公鑰與私鑰都傳送過來,並撰寫 Script 自動向DNS Server 更新IP,ddns_update.sh
如下
#!/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
設定每個小時自動執行
$ cd /etc/cron.hourly
$ ln -s /usr/local/ddns/ddns_update.sh ddns_update.sh