Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: HOWTO run an ssh server without a static IP address

  1. #1
    Join Date
    Aug 2005
    Beans
    37

    HOWTO run an ssh server without a static IP address

    HOWTO set up a ssh server with a dynamic ip address

    The problem: Your ISP changes your IP address and suddenly you can't use ssh me@201.202.203.204 to login to your computer until you get home and find out your new IP address.

    The solution: A domain name that always points to your constantly changing IP address. This will allow you to login to your computer with ssh username@mydomain.com even if your ISP changes your IP address frequently. That also means no need to remember those crazy IP address numbers.

    This HOWTO assumes you already know how to set up an ssh server and use port forwarding. If not see this tutorials:

    HOWTO set up the SSH server
    https://help.ubuntu.com/community/SSHHowto

    HOWTO secure your SSH server
    https://help.ubuntu.com/community/AdvancedOpenSSH

    HOWTO forward ports on your router for SSH
    http://www.portforward.com/english/a...H/SSHindex.htm

    Okay. First step. Get a domain name that points to your router.

    Create a FREE account at http://www.dyndns.com/

    Once you're registered, goto https://dyndns.com --> My account --> My hosts --> add a dynamic dns

    Pick a name like myname.homelinux.com or bananaflange.homeunix.net or whatever floats your boat.

    So far so good. But that domain name points to an IP address that your ISP could change tomorrow! If only there was a way to update this info automatically!

    There is! (1) Create a shell script to automatically get your external IP address and update your account at www.dyndns.com and (2) put that script in a /etc/cron.hourly where it will automatically be run every hour or /etc/cron.daily where it will run daily.

    Code:
    sudo gedit /etc/cron.hourly/dyndns.sh
    Fill in the blanks for your username and password at DynDNS.

    Code:
    #!/bin/bash
    # Set the username and password for the dyndns account
    USERNAME=
    PASSWORD=
    # Set the system being used to either static or dynamic DNS
    SYSTEM=dyndns
    # Set the hostname for the record to change
    DYNHOST=
    # Set whether to wildcard the DNS entry, i.e. *.$DYNHOST
    WILDCARD=OFF
    ############################################
    ## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
    ############################################
    if [ -z "$DYNDNS" ]; then
    DYNDNS="$DYNHOST"
    fi
    if [ -z "$DNSWILD"]; then
    DNSWILD="$WILDCARD"
    fi
    LOOKUP=`host $DYNHOST | awk '{print $4}'`
    MYIP=`curl -s http://www.ipchicken.com | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`
    # Do the work
    if [ "$LOOKUP" = "$MYIP" ]; then
    echo "No change in DNS entry."
    else
    echo `lynx -auth=${USERNAME}:${PASSWORD} -source "http://members.dyndns.org:8245/nic/update?system=${SYSTEM}&hostname=${DYNDNS}&myip=${MYIP}&wildcard=${DNSWILD}"`
    fi
    From now on you can login to your ssh server with ssh user@myname.homelinux.net (or another domain choice). Even if your ISP changes your IP address while you are away, things will right themselves within the hour.

    Edit: The script was changed after it got me temporarily suspended from dyndns.org. An error caused it to update the dns entry every time the script was run, leading to a terse email from dyndns.org regarding bandwidth. This has been corrected. Also, the site for ip lookup has been changed from dyndns.org to ipchicken.com.

    Bibliography:
    http://malvasiabianca.org/archives/2...nal-ip-address
    Last edited by celticmonkey; September 17th, 2006 at 03:26 AM. Reason: Code simplification!

  2. #2
    Join Date
    Apr 2005
    Location
    Philladelphia, PA
    Beans
    54
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: HOWTO run an ssh server without a static IP address

    Thanks for the howto!

    It wasn't updating for me, and I searched the forums and found that cron scripts should not have an extension (no .sh). See here:

    http://www.ubuntuforums.org/showthre...ht=cron.hourly

    Thanks again!

  3. #3
    Join Date
    Dec 2005
    Location
    Virginia, USA
    Beans
    32
    Distro
    Ubuntu 6.10 Edgy

    Re: HOWTO run an ssh server without a static IP address

    Thanks for the great tutorial! I never knew this could be so easy. But I did have to add one step to get mine working:

    chmod a+x /etc/cron.hourly/dyndns


  4. #4
    Join Date
    Jul 2006
    Location
    St. Louis, MO
    Beans
    20
    Distro
    Ubuntu 6.10 Edgy

    Re: HOWTO run an ssh server without a static IP address

    If you are interested in using your own domain name, zoneedit.com offers a free account with 5 free dns "zones" (for low traffic use) which work great for dynamic ips. I use the following in a cron entry to "ping" the server every half-hour.
    Code:
    wget -O - --http-user=myusername --http-passwd=mypassword 'http://dynamic.zoneedit.com/auth/dynamic.html?host=www.mydomainname.com'
    I use mine for for an apache2 with mod-musicindex setup so I can stream music to my workplace from home...

  5. #5
    Join Date
    Aug 2005
    Beans
    37

    Re: HOWTO run an ssh server without a static IP address

    I've rewritten the dyndns updater as a Python script. It has the added benefit of working cross platform.

    Just open a text editor, enter this code and save it as dyndns.py

    Code:
    #!/usr/bin/env python
    
    import os
    import socket
    import urllib
    import urllib2
    import re
    
    #Put your own username, password and dynhost name here.
    username='your_user_name'
    password='your_password'
    dynhost='yourdomain.whatever.com'
    system='dyndns'
    wildcard='OFF'
    
    ############################################
    ## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
    ############################################
    
    print '\nDyndns.org updater is running!!!\n'
    
    #Find out what dynamic IP is currently set to. 
    lookup=socket.gethostbyname(dynhost)
    print 'DynDNS is set to' , lookup
    
    #Find out what external IP really is.
    ipchicken = urllib2.urlopen('http://www.ipchicken.com')
    chicken = ipchicken.read(10000000)
    ipchicken.close()
    rawstr = r"""[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*""" #regex for an IP address
    myip_obj = re.search(rawstr, chicken)
    myip = myip_obj.group()
    print 'Your actual IP is' , myip
    
    #Compare those IPs
    if lookup == myip :
        print 'No change in DNS entry.'
    else:
        #change the DNS entry 
    
        data = {}
        data['system'] = system
        data['hostname'] = dynhost
        data['wildcard'] = wildcard
        data['myip'] = myip
        url_values = urllib.urlencode(data)
                
        url = 'http://members.dyndns.org:8245/nic/update'
        full_url = url + '?' + url_values
          
        # create a password manager for loging into dyndns.org
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    
        # Add the username and password.
        password_mgr.add_password(None, url, username, password)
    
        handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    
        # create "opener" (OpenerDirector instance)
        opener = urllib2.build_opener(handler)
    
        # use the opener to fetch a URL
        opener.open(full_url)
    
        print "DNS updated."

  6. #6
    Join Date
    Feb 2006
    Location
    USA
    Beans
    29
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: HOWTO run an ssh server without a static IP address

    Hi,

    I have just recently found your posts and I am now using your python script. It does work nicely. I am wondering however, if there is a way to get an email sent with the IP address details? I travel and dont always have a way to get the IP address if it has changed. So if the script could send a hourly or better daily email, I would always have an up to date IP address.

    Thanks,

    blumagic

  7. #7
    Join Date
    Mar 2006
    Location
    Kitakyushu Japan
    Beans
    9,362
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: HOWTO run an ssh server without a static IP address

    Quote Originally Posted by blumagic View Post
    Hi,

    I have just recently found your posts and I am now using your python script. It does work nicely. I am wondering however, if there is a way to get an email sent with the IP address details? I travel and dont always have a way to get the IP address if it has changed. So if the script could send a hourly or better daily email, I would always have an up to date IP address.

    Thanks,

    blumagic
    the WHOLE point of this howto is so you don't have to know your ip address ... ever. so, no matter where you are, you can always get to your server by url instead of needing to know your ip address. (eg: ssh user@yourname.dyndns.net, http://yourname.dyndns.net, ftp://yourname.dyndns.net ... and so forth)

    if ALL you want to know is your ip address while you're on the road, you can always do this:
    Code:
    ping yourname.dyndns.net

  8. #8
    Join Date
    Feb 2006
    Location
    USA
    Beans
    29
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: HOWTO run an ssh server without a static IP address

    Geez,

    I dont know why I didnt think to do that myself.
    That is just what I need.

    Thanks,

    blu.....

  9. #9
    Join Date
    May 2006
    Location
    Philadelphia, US
    Beans
    98
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: HOWTO run an ssh server without a static IP address

    This is really great...thank you!

    I have a question (for anyone with an understanding of python, and how the python script works: I often connect to my office over a pptp connection (using the network manager plugin). Anyone have any idea (code) on how I could modify the python script to check whether the pptp connection is active before updating my ip at dyndns? The issue is that if it updates while I'm connected over the pptp connection, it will set my ip address to my work's ip. Bad for a number of reasons, not least of which that it breaks things.

    Thanks.

    Bob

  10. #10
    Join Date
    Jun 2006
    Beans
    22

    Re: HOWTO run an ssh server without a static IP address

    As an even easier solution, some routers have dyndns support built right in, so instead of setting up cron, etc... you can configure your router to update dyndns whenever its IP changes.

    Ian

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •