本篇记录一下在 PVE 虚拟机下执行自己编写的脚本来实现DDNS外网访问功能

首先需要自行购买域名,有许多的域名提供商,个人比较推荐国外的域名提供商,下面列了几个比较常见的域名提供商。

Godaddy

Google Domains

NameCheap

NameSilo

因为我目前使用的是Godaddy 域名提供商,以下教程是对应的解决方法和教程,

当我们购买了域名,首先要做的是需要创建一个 API Key, 通过这个网址进行注册,https://developer.godaddy.com/keys

Untitled

点击创建API Key , 输入一个名字,以及Environment 需要选择 Production,点击Next

Untitled

生成成功,记录一下你自己的 Key 以及 Secret 信息

Untitled

然后在我的产品里面创建一条 DNS A记录

Untitled

Untitled

域名操作的部分已经完成,下面就开始在PVE虚拟机下进行操作,或者其他Linux系统都行

下面是一个需要自动执行的一个检测和更新DNS的一个脚本,需要替换的值,就是上面创建的Key ,Secret,A记录名字以及根域名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash

# This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection.
# Special thanks to mfox for his ps script
# https://github.com/markafox/GoDaddy_Powershell_DDNS
#
# First go to GoDaddy developer site to create a developer account and get your key and secret
#
# https://developer.godaddy.com/getstarted
# Be aware that there are 2 types of key and secret - one for the test server and one for the production server
# Get a key and secret for the production server
#
#Update the first 4 variables with your information

domain="xxx.com" # 你的根域名
name="xxxx" # 域名 A 记录
key="xxxxxxxxxxxxxxxxxxxxxxxxxx" # API-Key
secret="xxxxxxxxxxxxxxxxxxxxxxx" # API-Secret

# Main
headers="Authorization: sso-key $key:$secret"
result=$(curl -s -X GET -H "$headers" \
"https://api.godaddy.com/v1/domains/$domain/records/A/$name")
echo "result:" $result
# 当前DSN IP
dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
echo "dnsIp:" $dnsIp

# 国内IP查询服务

# https://api.ipify.org
currentIp=$(curl -s https://api.ipify.org)
echo "https://api.ipify.org currentIp:" $currentIp

# https://ipinfo.io/json
if [ ! -n "$currentIp" ]; then
ret=$(curl -s GET "https://ipinfo.io/json")
ret=$(echo $ret | grep -oE "\"\b([0-9]{1,3}\.){3}[0-9]{1,3}\b\"")
currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
echo "https://ipinfo.io/json currentIp:" $currentIp

# http://ip.42.pl/raw
if [ ! -n "$currentIp" ]; then
currentIp=$(curl -s GET "http://ip.42.pl/raw")
echo "http://ip.42.pl/raw currentIp:" $currentIp
fi
fi

# 如果不一致进行更新
if [ "$dnsIp" != "$currentIp" ]; then
request='[{"data":"'$currentIp'","service":"'$domain'","ttl":600}]'
echo $request
nresult=$(curl -i -s -X PUT \
-H "$headers" \
-H "Content-Type: application/json" \
-d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
echo $nresult
fi

复制这段脚本并修改相应的值保存到 PVE 虚拟机下的 /opt/shell/ 目录下(没有就创建文件夹) 脚本重命名为 godaddy-dns.sh

完成后需要修改 PVE下的自动执行相关逻辑设置,如下命令是编写系统自动周期性执行的相关逻辑

1
2
3
4
# 查看当前用户定时任务
crontab -l
# 编辑任务
crontab -e

编辑任务填写如下代码,意思是每5分钟就执行一次上面的脚本进行检测,使用方法可参见:**Linux crontab 命令**

1
2
# 每5分钟检查DNS解析是否正确
*/5 * * * * /opt/shell/godaddy-dns.sh
1
2
# 如果没有执行权限,添加一下权限
chmod +x godaddy-dns.sh

到此DDNS相关设置已经完成,后面需要你做的就是把自己服务相关的端口进行端口转发设置,

通过自己购买的域名添加上设置的端口就可以访问家中的服务了,这个地方有个前提,需要清楚家里

的网络连接设置情况,以及你目标服务的网段,如果是多个网段需要进行多次的端口转发。