利用ChangeIP.com的免费DNS服务,创建自己的动态DNS(DDNS)

动态DNS(DDNS),适用于动态VPS/独服,或自动更换IP的NAT VPS,免于自己更换IP。

一、在ChangeIP.com注册、创建DNS

打开 https://www.changeip.com/accounts/cart.php?a=add&pid=5,创建二级域名

二、在自己的VPS/独服上,使用脚本自动更新IP

1、在服务器上创建ddns.sh的脚本(假设在/root目录)

nano /root/ddns.sh

或者

vi /root/ddns.sh

2、粘贴下面的内容,注意修改11、12、13行为自己的信息,并保存。

#!/bin/bash
# Changeip.com Script for Linux base systems using bash
# Created to suite local need in specific case. Well tested with CENTOS 6.x n UBUNTU 12.x
# Syed . Jahanzaib . aacable at hotmail dot com
# October , 2015
# Uncomment below to view debug log
# set -x
 
# Various variables, make sure to change the changeip.com credentials and hostname as required.

CIPUSER=您在ChangeIP注册的邮箱
CIPPASS=您在ChangeIP的账号密码
CIPHOST=您在ChangeIP设置的域名

URL="https://nic.changeip.com/nic/update?ip"
# Temp files and log file to record updates if required
EXTIP="/tmp/externalip.txt"
NEWIP="/tmp/newip.txt"
LOG="/var/log/changeip.log"
DATE=`date`
 
# If external ip text file not found, then create one
if [ ! -e "$EXTIP" ] ; then
touch "$EXTIP"
fi
 
# Download current LIVE ip from intenret to match with old ip
wget -q -O /tmp/newip.txt http://ip.changeip.com:8245
a=`cat $EXTIP`
b=`cat $NEWIP  | cut -f 2 -d "=" | cut -f 1 -d "-" -s | grep -m 1 ^`
if [[ $a != "$b" ]]
then
 
# changeip update now
curl "$URL=$b&u=$CIPUSER&p=$CIPPASS&hostname=$CIPHOST"
 
# Update New WAN IP in OLD File
echo $b > $EXTIP
echo
 
# Print OLD vs New Comparision for REVIEW
echo Old IP = $a
echo New IP = $b
echo Update Time = $DATE
echo Update Done.
 
# Print OLD vs New Comparision for REVIEW in LOG FILE for later review
echo "*********************************************"  >> $LOG
echo New IP FOUND > $LOG
echo Old IP = $a >> $LOG
echo New IP found at $DATE and IP is = $b >> $LOG
 
# IF OLD and NEW ip are same, then no need to update, just EXIT
else
echo $DATE / No need to change IP. DDNS BASH Script / Powered by Syed Jahanzaib
fi
 
# END

3、修改脚本权限,并测试

chmod +x /root/ddns.sh
bash /root/ddns.sh

###返回下面的信息,表示脚本正常###

200 Successful Update
Old IP = 219.76.163.216
New IP = 220.246.88.57
Update Time = Sat Feb 8 22:11:04 CST 2020
Update Done.

设置定时更新,以便及时更换IP

crontab -e

粘贴下方命令并保存

*/1 * * * * /root/ddns.sh >/dev/null 2>&1 &

这样,当IP变化时,就可以在changip.com后台看到A记录的变化。也可以ping这个域名,获取变化后的IP。

转自:https://docs.anyhk.net/anyhk-nat/ddns-changeip

发表评论

电子邮件地址不会被公开。 必填项已用*标注