Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have recently installed ubuntu server (12.0.4) and intend to use it primarily as a file server. I am completely new to linux so this is a pretty big learning curve. Yesterday I was able to configure PuTTy on my Windows 7 machine using an ssh key pair so that I can administer ubuntu server from my desktop. This works fine provided that both machines are on the same network.

In the case of my laptop (MacBook Air) it might not always be on the same network, is it possible to be able to connect to the ubuntu server from my laptop via ssh when it is on a different network?

I have installed avahi daemon so that the hostname for the server is broadcast on the LAN and configured the router so that it will always assign the same IP to the server. Apart from that the only thing I have installed on the server is openssh where I have disabled password authentication so that you can only connect using a key pair.

I thought I would be able to do something like this from the terminal on my laptop:

ssh my.external.ip.address user@hostname.local

When I try that command I get the following error: ssh: connect to host my.external.ip.address port 22: operation timed out

I have also tried

ssh my.external.ip.address user@servers.local.ip.address

and I get the same error message as before. So that should give you an idea of what I am trying to do but currently I do not know if this is possible and if it is how to do it. Any instructions to configure this would be appreciated, thanks.

Edit:

I should also add that assuming I can set up an external connection via ssh from my laptop, there is a possibility that my ISP will change my external IP which would break the external connection. I would like to be able to connect robustly that is; if my ISP changes the external IP I would still be able to connect remotely to the server without having to know what the new external IP address is.

share|improve this question

2 Answers 2

up vote 14 down vote accepted

First of all, the correct command is: ssh user@my.external.ip.address

And the router should be configured to forward the SSH port 22 to your server's local IP address.

For further debugging:

1) Check that port 22 (SSH) is open on your server and on the router (port forwarding).

2) Check that the SSH server is running on your server

3) Use ping, ssh -v while connecting and look at /var/log/auth.log to debug any further connection problems.

1) On your router: follow router specific instructions

On your server: sudo ufw status (unless you use another firewall configuration utility) or sudo iptables -L (general method, but complex output)

To open port 22: sudo ufw allow 22

cf https://help.ubuntu.com/12.04/serverguide/firewall.html

2) Check it is installed: dpkg -l openssh-server

Check it is running: service ssh status or ps aux | grep sshd

3) On the connecting client:

  • ping my.external.ip.address
  • ssh -v user@my.external.ip.address

On the server:

  • sudo less /var/log/auth.log

You can check the router logs as well if necessary.

Here's an online port scanner: https://www.grc.com/x/ne.dll?bh0bkyd2

I think you can use tools like nmap or other as well, but I'm not that familiar with them yet.

Dealing with a changing external IP address:

1) Get a dynDNS or similar account: http://dyn.com/dns/

Lists of dynamic DNS providers:

2) Another solution, is to set up a crontab job, which regularly mails you your external IP address or puts in into an online storage service like dropbox.

Here's a script a friend of mine uses:

    #!/bin/bash
    # Bash script to get the external IP address
    MYWANIP=$(curl http://mire.ipadsl.net | sed -nr -e 's|^.*<span class="ip">([0-9.]+)</span>.*$|\1| p')
    echo "My IP address is: $MYWANIP"

    IPold=$(cat /home/USER/Dropbox/test.txt)
    echo "Previous IP Address: $IPold"

    if [[ $IPold != $MYWANIP ]] ;
    then
      echo "New IP"
      rm /home/USER/Dropbox/test.txt
      echo $MYWANIP >> /home/USER/Dropbox/test.txt;
      echo $MYWANIP;
    else
      echo "Same IP";
    fi

    # example crontab entry:
    ## m h  dom mon dow   command
    ## */10 * * * * /home/USER/Dropbox/test_ip.sh
share|improve this answer
    
Thanks for this, I will run through the suggestions now. –  Aesir Aug 29 '12 at 10:59
    
Ok all I had to do was forward port 22 on my router to the server and it works, thanks a lot. Is there a way I can solve the second part of my question regarding the fact that my external IP address might change? - Just seen your edit, will read up now. –  Aesir Aug 29 '12 at 11:31

By default in Ubuntu (Desktop) SSH is not installed.

You can install it by the following command in Terminal:

sudo apt-get install openssh-server
share|improve this answer
    
+1. This worked great when I tried it. I was able to ssh to the Ubuntu machine right after running that command (for Ubuntu 14.04 (Trusty Tahr), 64-bit Desktop edition). –  Peter Mortensen 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.