博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

山永军的博客

jackshan.cublog.cn


DDNS的简单实现
一:原理说明

DNS服务器颁发Key给需要解析的主机,主机通过Key来与DNS服务器进行交互,更新最后的IP地址。挺简单吧。而应用的软件就是Bind,看看。
[root@kook DDNS]# rpm -qi bind
Name        : bind                         Relocations: (not relocatable)
Version     : 9.3.3                             Vendor: CentOS
Release     : 7.el5                         Build Date: 2007年03月14日 星期三 20时10分48秒
Install Date: 2007年09月06日 星期四 19时26分18秒      Build Host: builder4.centos.org
Group       : System Environment/Daemons    Source RPM: bind-9.3.3-7.el5.src.rpm
Size        : 2189509                          License: BSD-like
Signature   : DSA/SHA1, 2007年04月04日 星期三 08时20分21秒, Key ID a8a447dce8562897
URL         : http://www.isc.org/products/BIND/
Summary     : Berkeley 互联网域名(BIND)DNS(域名系统)服务器。
Description :
BIND (伯克利互联网域名) 是 DNS(域名系统)协议的一种实现。
BIND 包括一个 DNS 服务器(named),它把主机名解析为 IP 地址;
一个解析器库(在与 DNS 通讯时应用程序所使用的例程);以及检
验 DNS 服务器是否被正确操作的工具。


二:实现
过程如下:

Server:
1:DNS Server产生密钥,用于解析的客户机的分发。
2:调整配置文件
Client:
1:从Server获取密钥
2:更新IP


Server 端操作如下:
1:产生密钥,应用dnssec-keygen
[root@kook tt]# dnssec-keygen -a HMAC-MD5 -b 128 -n HOST kook
Kkook.+157+53326
[root@kook tt]# ll
-rw------- 1 root root 48 09-26 13:18 Kkook.+157+53326.key
-rw------- 1 root root 81 09-26 13:18 Kkook.+157+53326.private
查看一下这个命令的用法
[root@kook tt]# dnssec-keygen
Usage:
   dnssec-keygen -a alg -b bits -n type [options] name

Version: 9.3.3rc2
Required options:
   -a algorithm: RSA | RSAMD5 | DH | DSA | RSASHA1 | HMAC-MD5
   -b key size, in bits:
       RSAMD5:         [512..4096]
       RSASHA1:                [512..4096]
       DH:             [128..4096]
       DSA:            [512..1024] and divisible by 64
       HMAC-MD5:       [1..512]
   -n nametype: ZONE | HOST | ENTITY | USER | OTHER
   name: owner of the key
Other options:
   -c <class> (default: IN)
   -e use large exponent (RSAMD5/RSASHA1 only)
   -f keyflag: KSK
   -g <generator> use specified generator (DH only)
   -t <type>: AUTHCONF | NOAUTHCONF | NOAUTH | NOCONF (default: AUTHCONF)
   -p <protocol>: default: 3 [dnssec]
   -s <strength> strength value this key signs DNS records with (default: 0)
   -r <randomdev>: a file containing random data
   -v <verbose level>
   -k : generate a TYPE=KEY key
Output:
    K<name>+<alg>+<id>.key, K<name>+<alg>+<id>.private
查看文件内容,
[root@kook tt]# more Kkook.+157+53326.key
kook. IN KEY 512 3 157 do/q08XmwO1V9PLaS89N2w==
[root@kook tt]# more Kkook.+157+53326.private
Private-key-format: v1.2
Algorithm: 157 (HMAC_MD5)
Key: do/q08XmwO1V9PLaS89N2w==

2:编辑配置文件/etc/named.conf
下面的字段非常容易理解。
第一段是Key,就是上面生成的key转换到/etc/named.conf写法。
包括加密类型和那个密文,就是上面文件里面的。
第二段是域下面可以通过这个key来更新的主机地址。也就是说那些拥有这个Key的人,可以更改下面那三个主机的A类型。

*************略*****************
key "kook" {
       algorithm hmac-md5;
       secret "d6GR0xmw5sKRirUkFMwPYQ==";
};
zone "52zhe.com" IN {
       type master;
       file "named.52zhe";
       update-policy {
             grant kook name kook.52zhe.com. A;
             grant kook name office.52zhe.com. A;
             grant kook name home.52zhe.com. A;
       };
};
***************略*******************


客户端:

1:将DNS服务器内的那2个Key文件拷贝到本地,不管用什么办法。
[root@kook ~]#mkdir ddns
[root@kook ~]#scp  -r 远端文件   .
[root@kook ~]#cd ddns
[root@mail DDNS]# nsupdate -k Kkook.+157+53326.key
> server  111.222.33.44 (Your DNS IP)
> update delete kook.52zhe.com
> update add kook.52zhe.com 0 A  2.3.4.5 ( Your Client Host IP)
> send
>  
最后在此按下 [ctrl]+D 即可

客户端的一个简单脚本,添加到Cron。

[root@mail DDNS]#vi ddns.sh
#!/bin/bash
#
#Update your Dynamic IP by using BIND 9's tools
#
#######################################################
#History
#2005/07/08  liujia  0.1 First time release
#######################################################
#
#
basedir="/opt/DDNS/"
keyfile="$basedir"/"Kmail.+157+53638.key"
ttl=600
#subip="eth0"
outif="ppp0"
hostname="kook.52zhe.com"
servername="223.94.13.79"

#Get you new IP
newip=`/sbin/ifconfig "$outif" | grep 'inet addr' | cut -d: -f 2  | cut -d' ' -f1`
#newip=`/sbin/ifconfig "$subip" |grep 'inet addr' | awk '{print $2}' | sed-e "s/addr\://"`

#checkip=`echo $newip | grep "^[0-9]"`
#if["$checkip"==""];then
#       echo "$0: The interface isn't connect internet..."
#       exit 1
#fi

# create the temp file
tmpfile=$basedir/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 you IP to server

nsupdate -k $keyfile -v $tmpfile

[root@mail DDNS]# crontab -e
*/5 * * * * /opt/DDNS/ddns.sh


后续补充:
成功标志:会在服务端named的工作目录内产生一个文件named.52zhe.jnl ,这个文件不可读取。随着客户机的更新,会发生变化。

 原文地址 http://www.52zhe.cn/read.php?102
发表于: 2008-01-07 ,修改于: 2008-01-07 17:40,已浏览485次,有评论0条 推荐 投诉


网友评论

发表评论