# 自建DDNS說明-ChangeIP

## 由於網路安全原因，我們停止了免費域名（DDNS）的解析，但用戶仍然可以通過自建DDNS獲取伺服器的外網IP。

## 下面我們將使用changeip.com的免費DDNS服務，建立您的DDNS。

#### ​1. 打開下方鏈接登記你的DDNS

<https://www.changeip.com/accounts/cart.php?a=confproduct&i=1>

![](/files/-M-ZxVK7fObNpd2t9pUH)

![](/files/-M-Zy1dvuX7WL_vOqW6y)

![](/files/-M-ZyGt7Rpz-X1Hj9t58)

![](/files/-M-ZyqHBuEFKovR7wL2d)

![](/files/-M-ZzsdoS5ujLFG2XoRC)

![](/files/-M-_-9R158LRAnxPk_ND)

![](/files/-M-_-OHodKk33bD1iElM)

![](/files/-M-_11ABwR75COBAyxhK)

![](/files/-M-_1FRjy2X_TMP9Y4n9)

![](/files/-M-_1wfixVqtaAsElAsR)

![](/files/-M-_2dNCa04sRxAyJPuv)

{% hint style="info" %}
到此，您已經獲得一個免費域名`ip.yourname.ddns.info`
{% endhint %}

#### 2. 使用腳本讓VPS自動更新DDNS

登入您的vps終端新建一個名為`ddns.sh`的腳本

```
nano /root/ddns.sh

或者

vi /root/ddns.sh
```

黏貼下面的腳本，注意修改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="http://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 -4 "$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
```

修改腳本權限，並測試腳本是否有效

```
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.
```

設定定時更新

```
crontab -e
```

貼上下方命令並保存

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

{% hint style="success" %}
至此所有步驟完成，當IP變更時腳本會自動更新A紀錄指向您的域名

您可以登入changeip.com後台查看域名IP

並可以使用您的DDNS連接您的VPS
{% endhint %}

{% hint style="info" %}
需要注意的是DDNS生效時間存在地區差異，請耐心等待DNS更新。
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.anyhk.net/anyhk/ddns/ddns-changeip.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
