如何在Ubuntu服务器上设置动态DNS获取服务?

我已在我的http://no-ip.org/帐户中创建了帐户和主机名,这是迄今为止我使用最常见的服务器类型服务。有人可以指导我如何在命令行上使用ddclient与no-ip进行配置吗?

3个回答

设置 ddclient 以使用 No-IP 动态 DNS 服务

ddclient 是一个动态更新客户端 (DUC),可用于更新动态 DNS 条目。它会定期检查当前 IP 地址,并在检测到更改时更新 DNS 信息。以下是如何安装和配置它以使用 No-IP (noip.com/no-ip.com) 服务的方法。

首先,安装 ddclient 包。稍后我们将手动配置它,所以对于所有配置问题,只需按回车键即可。

sudo apt-get install ddclient

停止 ddclient 守护进程。由于 Ubuntu 12.04 存在一个 bug,所以 sudo service ddclient stop 命令无法正常工作,我们改用 pkill 命令来代替。
sudo pkill ddclient

编辑配置文件。要关闭备份文件(例如/etc/ddclient.conf~),这是gedit默认创建的,可能包含你认为已经删除的密码,请关闭以下设置:Edit -> Preferences -> Editor -> Create a backup copy of files before saving
sudo gedit /etc/ddclient.conf /etc/default/ddclient

删除/etc/ddclient.conf文件中的旧内容,并粘贴下面显示的模板。

阅读模板的注释并根据您的需求自定义文件。

使用noip.com子账户可能是一个好主意,即带有密码的组(此功能是付费服务)。这样,密码只能用于更新指定的主机,而不能完全访问您的整个No-IP账户,其中可能包括MX记录(攻击者如果获得您的密码,可以更改MX记录以截取您的电子邮件)或其他服务,如IMAP账户。

您应该将daemon_interval=3600设置为适当的值(请参考模板注释)。

使用以下命令测试您的配置:

sudo ddclient -daemon=0 -debug -verbose -noquiet -force

你应该得到一个“good”(IP地址已更新)或“nochg”(IP地址已设置为该值)的响应。此时接收到以下警告是可以的(但在正常操作期间持续收到警告是不可以的):
警告:更新:nochg:不需要更新;试图将当前地址更改为当前地址被视为滥用行为。
注意:No-IP服务器似乎存在一些缓存,所以如果你从服务器得到的响应与你预期的不符,等待一段时间可能会有帮助。
配置完成后,启动ddclient守护进程。
sudo service ddclient start

每次启动计算机时,守护进程也会自动启动。
每当检测到IP地址更改时,DNS条目/条目将被更新。
故障排除:
再次运行软件包配置。
sudo dpkg-reconfigure ddclient

删除软件包和配置文件:
sudo apt-get purge ddclient

守护进程控制:
sudo service ddclient status
sudo service ddclient start
sudo service ddclient stop
# The stop command above does not work on Ubuntu 12.04 due to a bug, but
# it works on 14.04. Here are alternative ways to control the daemon:
ps -A f | grep -i ddclient
sudo pkill ddclient

删除缓存以欺骗守护程序更新DNS条目,用于调试目的(在正常操作中,只有当当前IP地址与缓存中存储的IP地址不同时,才会更新DNS条目)。
sudo pkill ddclient
sudo rm /var/cache/ddclient/ddclient.cache
sudo service ddclient start
# See the result:
tail /var/log/syslog
# If you have set up mail=..., you should also receive an e-mail.
# The syslog and e-mail should show a "good" or "nochg" response.

测试守护程序在IP地址更改时是否会实际更新DNS条目,方法是将DNS条目的IP地址设置为1.2.3.4,然后启动守护程序。
sudo pkill ddclient
sudo ddclient -daemon=0 -debug -verbose -noquiet -force -use ip -ip 1.2.3.4
sleep 30   # ddclient will not perform updates less than 30 seconds apart
sudo service ddclient start

调试运行(更新 DNS 记录永不/根据需要/始终):

sudo ddclient -daemon=0 -debug -verbose -noquiet -noexec
sudo ddclient -daemon=0 -debug -verbose -noquiet
sudo ddclient -daemon=0 -debug -verbose -noquiet -force

尝试使用各种方法检测当前IP地址:
sudo ddclient -query

感兴趣的文件:
/etc/ddclient.conf
/etc/default/ddclient
/var/cache/ddclient/ddclient.cache

来自守护程序的消息将显示在这里(如果设置了发送电子邮件,则也会通过电子邮件发送):
/var/log/syslog

文档:

/etc/ddclient.conf模板

################################################################################
# Configuration file for ddclient
################################################################################


################################################################################
# Misc. configurations

# To choose between ipup mode (for dial-on-demand) and daemon mode, please edit
# /etc/default/ddclient.

#mail=root           # E-mail messages to this address
#mail-failure=root   # E-mail messages about failed updates to this address


################################################################################
# How to obtain current IP address

# Methods:
#   use=ip, ip=ADDRESS                                   # Set the IP address to ADDRESS
#   use=if, if=INTERFACE, if-skip=PATTERN                # Obtain IP address by calling 'ifconfig INTERFACE'
#   use=web, web=PROVIDER|URL, web-skip=PATTERN          # Obtain IP address from IP discovery web page
#   use=cmd, cmd=PROGRAM, cmd-skip=PATTERN               # Obtain IP address by calling PROGRAM
#   use=fw|FWMODEL, fw=ADDRESS|URL, fw-skip=PATTERN      # Obtain IP address from firewall web page
#                   fw-login=LOGIN, fw-password=SECRET
#
# The *-skip options can be used to skip IP addresses found before PATTERN.
#
# NOTE: The ddclient IP detection routines do not respect the HTTPS prefix in
# URLs, and instead fall back to HTTP (this is fixed in version 3.8.1). The
# option ssl=yes does not apply to the IP detection either (this is at least
# how versions 3.8.0 to 3.8.2 behave).
#
# The default time between each IP address change check is daemon_interval=300
# (5 minutes), which may be a bit too frequent when using an external IP
# discovery service. To set the checking interval to e.g. one hour instead,
# please edit /etc/default/ddclient and set daemon_interval=3600.

# Obtain IP address from network interface
#use=if, if=eth0

# Obtain IP address from no-ip.com IP discovery web page (unencrypted connection)
use=web, web='http://ip1.dynupdate.no-ip.com:8245/'

# Obtain IP address from no-ip.com IP discovery web page (encrypted connection)
# Encryption does not offer any real protection since a man-in-the-middle
# attack could route the HTTPS connection through a host belonging to the
# attacker, thus causing an incorrect IP address to be reported.
#use=cmd, cmd='bash -c "set -o pipefail; timeout 120 wget -qO- \
#  --header=Host:ip1.dynupdate.no-ip.com https://dynupdate.no-ip.com/ | head -c 1000"'


################################################################################
# Dynamic DNS service setup

protocol=noip
#server=   # Defaults to dynupdate.no-ip.com for noip protocol
ssl=yes   # NOTE: Does not apply when obtaining IP address, just when updating the DNS entry!
login='nobody@nowhere.com'   # Username. For No-IP sub-accounts (groups with passwords), use '<groupname>%3A<username>'.
password='1234'


################################################################################
# Hosts to update, with optional per-host options

# Examples:
#   protocol=hammernode1, \
#   login='my-hn-login', password='my-hn-password' \
#   myhost.hn.org,myhost2.hn.org
#
#   login='group2%3Ajohndoe@domain.com', password='1234'   myhost3.no-ip.biz,myhost4.no-ip.biz
#
#   myhost.no-ip.biz,myhost2.no-ip.biz

my.full.hostname

更新:加密连接到IP检测服务是毫无意义的。现在在ddclient.conf模板中已经解释了这一点,并且默认使用非加密连接进行IP检测。


1这并没有解决原帖中关于No-IP特定配置的问题。 - nrobey

No-ip有自己的客户端,第一次安装就能正常使用。
他们的网站上有下载客户端的说明,然后只需快速执行make && make安装即可完成。在配置向导中,准备好你的no-ip登录凭据,因为会要求输入。之后,确保运行noip2并在他们的网站上检查你的主机账户是否已更新。

警告:noip2存在安全问题!请参阅此答案 - Håkon A. Hjortland
@HåkonA.Hjortland:我没意识到它接受HTTPS连接。我已经删除了之前不正确的陈述,谢谢! - MestreLion