# DDNS 的设置问题：外网访问内网服务器全记录

> 发布时间: 2026-08-02 18:27

# DDNS 的设置问题

## 序言

为了使手上所有硬盘物尽其用，故重启了 AX9000 的 docker 使用计划，预期通过 DDNS 建立外网对内的访问实现内网服务器数据库的野望

## 涉及知识点

1. portainer 使用
2. DDNS
3. ubuntu 基础
4. shell 命令
5. mysql
6. ufw

## portainer 使用

### portainer 简介

portainer 是 docker 的可视化操作平台

### portainer 主要操作

1. local/Host/registries 是设置储存库账号的地方，设置储存库后可以远程拉取已有的程序模板直接安装使用
2. local/Containers 中的 portainer 是本页面使用的 docker 不能随便修改，不然可能存在页面无法访问的情况
3. local/NetWorks 中的三种网络模式已经够用了，虽然安装 mysql 时端口映射一直失败未找到原因，但是安装 ubuntu 时，主动分配了端口
4. Container details 页面中 "Duplicate\Edit" 可以修改应用的高级设置，但是每一次设置都是删除现有 Container 重新新建，也就是现有数据会丢失
5. 目前尚未发现路由器重启对 docker 的影响

## DDNS

### 基于 namesilo 的 DNS API 测试

1. 通过官方给定的查询 API 可以查询到 DNS 数据然而修改 API 则无法进行修改，原因未知
2. 可以直接使用 CURL 进行操作，操作简洁

### 基于腾讯的 DNS API 测试

1. 需要构建 DNS 服务器，所幸 API Explorer 可以生产测试代码，难受的是需要下载他那贼大贼大的 SDK 库
2. vs 的跨平台就是个笑话，所有 ubuntu 的编译环境都需要重新搭建，并未感觉到便利之处

```shell
# 安装流程
git clone https://github.com/TencentCloud/tencentcloud-sdk-cpp

apt-get install gcc
apt-get install g++
apt-get install cmake
apt-get install libcurl4-openssl-dev
apt-get install libssl-dev
apt-get install uuid-dev
cd tencentcloud-sdk-cpp
vim build.sh
## 在cmake .. 中间添加-DBUILD-MODULES="dnspod"
chmod -755 build.sh
./build.sh

git clone git@github.com:3319952332/DDNSCode.git
```

## ubuntu 基础

### 开启 ssh 服务

```
apt-get update
apt-get install vim ca-certificates openssh-server mysql-server net-tools git -y
vim /etc/apt/sources.list
unminimize
apt-get update
apt-get upgrade
vim /etc/ssh/sshd_config
# 注释掉PermitRootLogin prohibit-password选项
# 添加PermitRootLogin yes
/etc/init.d/ssh restart
passwd

# 安装sql
apt-get install mysql-server
# 安装网络工具箱
apt-get install net-tools 
# 卸载
apt-get purge 包名
```

#### REMOTE HOST IDENTIFICATION HAS CHANGED 问题解决

第一次使用 SSH 连接时，会生成一个认证，储存在客户端的 known_hosts 中，需要清理掉 .ssh 中的该文件

### 切换下载镜像

修改 /etc/apt/sources.list 文件，使用 gg，dG 可以清空文件

**清华源**：
```
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
```

**阿里源**：
```
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
```

**中科大源 / 网易 163 源**：
```
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
```

## shell 命令

```
ssh:
-p 端口号设置
-l 登录用户名
Ctrl+D 断开连接

scp:
上传或下载文件

建立密钥对:
cd .ssh
ssh-keygen
cat id_rsa.pub >> authorized_keys
vim /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
PasswordAuthentication no 

chmod:
权限修改
注：id_rsa 文件权限应为 700，否则会失效
```

开机自启脚本：
```bash
#!/bin/bash
echo "Start Log Begin CurTime $(TZ=UTC-8 date "+%Y-%m-%d %H:%M:%S")" >> ~/start.log
SSHCNT=$(ps -ef | grep ssh | grep -v "grep" | wc -l)
MYSQLCNT=$(ps -ef | grep mysql | grep -v "grep" | wc -l)
if [ $SSHCNT -eq 0 ]; then
        echo "SSH not running try start " >> ~/start.log
        /etc/init.d/ssh restart &
else
        echo "SSH running" >> ~/start.log
fi
if [ $MYSQLCNT -eq 0 ]; then
        echo "MYSQL not running try start " >> ~/start.log
        service mysql restart &
else
        echo "MYSQL running" >> ~/start.log
fi

echo "Start Log End" >> ~/start.log
exit 0
```

## mysql

### 安装

使用 `apt-get install mysql-server` 安装

### 配置

```
#启动mysql服务
service mysql start
# 打开mysql
mysql -uroot -proot 
# 使用mysqldatabase
use mysql 
# 查询用户
select host, user, authentication_string, plugin from user;
# CREATE USER '用户名'@'host' IDENTIFIED BY '密码'
CREATE USER 'wang'@'192.168.1.3' IDENTIFIED BY '密码';
# 修改密码验证方式
ALTER USER 'wang'@'192.168.1.3' IDENTIFIED WITH mysql_native_password BY '密码';
# 设置账号可以远程登录
GRANT ALL PRIVILEGES ON *.* TO 'wang'@'192.168.1.3';
# 刷新权限
flush privileges;
```

### 对于远程连接无响应的问题的解决方案

通过 netstat -aptn 查询 mysql 的监听地址为 127.0.0.1:3306，而 ssh 的监听地址为 0.0.0.0:22，因此需要将 mysql 的监听地址改为 0.0.0.0，通过修改 /etc/mysql/my.cnf，在任意位置添加如下文本重启服务即可：

```
[mysqld]
bind-address=0.0.0.0
```

```
# 重启服务
service mysql restart
```

**0.0.0.0、localhost 和 127.0.0.1 的区别**：
在服务器中，0.0.0.0 指的是本机上的所有 IPV4 地址，是真正表示"本网络中的本机"。一般我们在服务端绑定端口的时候可以选择绑定到 0.0.0.0，这样我的服务访问方就可以通过我的多个 ip 地址访问我的服务。
在路由中，0.0.0.0 表示的是默认路由，即当路由表中没有找到完全匹配的路由的时候所对应的路由。
而 127.0.0.1 是本地回环地址中的一个，大多数 windows 和 Linux 电脑上都将 localhost 指向了 127.0.0.1 这个地址，相当于是本机地址。
localhost 是一个域名，可以用它来获取运行在本机上的网络服务。
在大多数系统中，localhost 被指向了 IPV4 的 127.0.0.1 和 IPV6 的 ::1。

## ufw

### 描述

* ubuntu 防火墙，底层还有一个 iptables，ufw 似乎只适用于 iptables-legacy，所以需要先安装 iptables 再改为 iptables-legacy

### 操作

```
# 重新配置ufw
apt-get purge ufw iptables
apt-get install iptables
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
apt-get install ufw
# 为ufw设置默认值
ufw default allow|deny
```

## nmap

### 描述

* 端口扫描工具

## 后续需要完成的

1. ubuntu 上搭设代理保证 git 可以正常下载
2. ubuntu 下载 mysql 并开放 3306 端口
3. 继续测试等下 API