树莓派如果想做一些事情,搭一些服务,首要就是随时知晓自己的公网IP,但是国内大多数都是动态IP,所以我们就需要DDNS服务了。这不是第一次接触No-ip的DDNS服务,第一次接触大概是之前给某路由器刷梅林固件之后,接触到的。他家的提供3个免费二级域名,且需要每30天去官网更新一次,不过会提前发邮件提醒,收到邮件去更新就好了!官网链接

 

设置DMZ/端口转发

要在公网访问内网设备,就需要网关的端口或者DMZ转发我们先进入到路由器管理平台,为树莓派指定一个固定IP,然后在转发规则下设置DMZ主机将树莓派ip添加设置,重启生效。

安装Noip客户端

apt-get install gcc make -y
apt-get install wget -y
wget https://www.noip.com/client/linux/noip-duc-linux.tar.gz
tar zxf noip-duc-linux.tar.gz
cd noip-2.1.9-1
make install

安装过程中,按照提示输入noip帐号信息,时间间隔建议设为5min,默认30min

Auto configuration for Linux client of no-ip.com.

Please enter the login/email string for no-ip.com  这里输入NO-IP账号
Please enter the password for user 'NO-IP账号'  这里输入NO-IP账号密码

3 hosts are registered to this account.
Do you wish to have them all updated?[N] (y/N)  输入Y/N进行DDNS域名选择
Do you wish to have host [XXXX] updated?[N] (y/N)  
Do you wish to have host [XXXX] updated?[N] (y/N)  
Do you wish to have host [XXXX] updated?[N] (y/N)  
Please enter an update interval:[30]  5
Do you wish to run something at successful update?[N] (y/N) 

设置开机启动

/etc/init.d/中建立noip2脚本

vi /etc/init.d/noip2

脚本内容如下

#! /bin/sh

### BEGIN INIT INFO
# Provides:          noip2
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: noip.com client service
### END INIT INFO

# . /lib/lsb/init-functions
case "$1" in
    start)
        echo "Starting noip2."
        /usr/local/bin/noip2
    ;;
    stop)
        echo "Shutting down noip2."
        killall noip2
        #killproc /usr/local/bin/noip2
    ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac

exit 0

设置开机启动

chmod +x /etc/init.d/noip2
update-rc.d noip2 defaults

启动之后我们可以用命令来查看运行状态:

/usr/local/bin/noip2 -C       #配置一个客户端
/usr/local/bin/noip2          #运行一个客户端
/usr/local/bin/noip2 -S       #显示有关正在运行的客户端的信息
/usr/local/bin/noip2 -D pid   #切换客户端pid的调试状态
/usr/local/bin/noip2 -K pid   #终止客户端pid

至此设置完成,可以ping一下自定义域名,看ip是正确,以后就可以通过自定义域名在公网访问树莓派了!

参考自官方 README 文件。原文如下:

This file describes noip2, a second-generation Linux client for the 
no-ip.com dynamic DNS service.

NEW:    This code will build and run on Solaris/Intel and BSD also.
    Edit the Makefile for Solaris and the various BSDs.
    For BSD users wanting to use a tun interface, see below.
    Let me know about any other changes needed for noip2 to 
    operate correctly on your non-Linux OS.
    Mac OS X is a BSD variant. 

Please read this short file before using noip2. 

###########################################################################
HOW TO BUILD AN EXECUTABLE FOR YOUR SYSTEM

The command 
    make 
will build a binary of the noip2 client that will run on your system.

If you do not have 'make' installed and you have an i686 Linux machine 
with libc6, a binary for i686 systems is located in the binaries 
directory called noip2-Linux. Copy that binary to the build directory 
  'cp binaries/noip2-Linux noip2'

The command
    make install
(which must be run as root) will install the various pieces to their
appropriate places.  This will ask questions and build a configuration 
data file.  
See below if you can't become root or can't write in /usr/local/*.

###########################################################################
HOW TO USE THE CLIENT WITHOUT READING THE REST OF THIS TEXT

Usual operation?
/usr/local/bin/noip2 -C         configure a client
/usr/local/bin/noip2            run a client
/usr/local/bin/noip2 -S         display info about running clients
/usr/local/bin/noip2 -D pid     toggle the debug state for client pid
/usr/local/bin/noip2 -K pid     terminate client pid

Have more than one internet access device? 
/usr/local/bin/noip2 -M -c file     start additional instances

###########################################################################
HOW TO START THE CLIENT

The noip2 executable can be run by typing /usr/local/bin/noip2

If you want it to run automatically when the machine is booted, then
place the following script in your startup directory. (/etc/init.d/rcX.d
or /sbin/init.d/rcX.d or ???) 

    #######################################################
    #! /bin/sh
    # . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc
    case "$1" in
        start)
        echo "Starting noip2."
        /usr/local/bin/noip2
        ;;
        stop)
        echo -n "Shutting down noip2."
        killproc -TERM /usr/local/bin/noip2
        ;;
        *)
        echo "Usage: $0 {start|stop}"
        exit 1
    esac
    exit 0
    #######################################################

Where the 'X' in rcX.d is the value obtained by running the 
following command
    grep initdefault /etc/inittab | awk -F: '{print $2}'

Killproc can be downloaded from ftp://ftp.suse.com/pub/projects/init
Alternatively, you can uncomment the line after #! /bin/sh

If you have a recent RedHat version, you may want to use the startup script
supplied by another user.  It's in this package called redhat.noip.sh
It may need some modification for your system.

There is a startup script for Debian called debian.noip2.sh.
It also has been supplied by another user and is rumored to fail in some
situations.

Another user has supplied a proceedure to follow for MAc OS X auto startup.
It's called mac.osx.startup.  Mac users may wish to read that file.

Here is a script which will kill all running copies of noip2.
  #!/bin/sh
  for i in `noip2 -S 2>&1 | grep Process | awk '{print $2}' | tr -d ','`
  do
    noip2 -K $i
  done
These four lines can replace 'killproc' and 'stop_daemon' in the other scripts.

If you are behind a firewall, you will need to allow port 8245 (TCP) through
in both directions.
#######################################################################

IMPORTANT!!  Please set the permissions correctly on your executable.
If you start noip2 using one of the above methods, do the following:
chmod 700 /usr/local/bin/noip2
chown root:root /usr/local/bin/noip2
If you start noip2 manually from a non-root account, do the chmod 700 as 
above but chown the executable to the owner:group of the non-root account, and
you will need to substitute your new path if the executable is not in 
/usr/local/bin.

###########################################################################
SAVED STATE

Noip2 will save the last IP address that was set at no-ip.com when it ends.  
This setting will be read back in the next time noip2 is started. The
configuration data file must be writable for this to happen!  Nothing
happens if it isn't, the starting 0.0.0.0 address is left unchanged.
If noip2 is started as root it will change to user 'nobody', group 
'nobody'.  Therefore the file must be writeable by user 'nobody' or
group 'nobody' in this case!

###########################################################################
BSD USING A TUN DEVICE

Recent BSD systems will use getifaddrs() to list ALL interfaces.  Set the 
'bsd_wth_getifaddrs' define in the Makefile if using a version of BSD 
which supports getifaddrs() and ignore the rest of this paragraph. 
Mac OS X users should have a versdion of BSD which supports getifaddrs().

发表评论