Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Raspberry Pi (RPi) and I am making remote connections to it using ssh. I have managed to set up ssh correctly such that I can access the RPi both from a local area network and from the internet (using a specific port that I opened on my router).

Assuming an user name john and a RPi named raspi:

Inside LAN access

ssh john@192.168.2.7
ssh john@raspi
ssh raspi

Outside LAN access

ssh -p 1234 john@12.345.67.89
ssh -p 1234 12.345.67.89

But how can I simply do ssh raspi from outside my LAN?. Is there a way to configure raspi to point to two IP-adresses, one in a LAN and one over the internet?

What I basically want is to access my RPi in a single way, no matter whether I am at home or work.

share|improve this question
    
You could run a DNS server on your local lan which responds to the request for the "raspi" name with the local lan ip address. Now resolving that same name to a different outside address would require that name be populated (dynamic dns) in such a way that it also resolves. But you will probably need a longer name than "raspi". –  ChuckCottrill Dec 30 '13 at 23:31
    
See this Q&A: unix.stackexchange.com/questions/61655/… –  slm Dec 31 '13 at 2:56

2 Answers 2

up vote 4 down vote accepted

Looking closer at your question, it appears you're using the same computer from both in- and outside of the LAN. I have revised my answer accordingly:

In your ~/.ssh/config, add:

Host raspi-wan
    HostName 12.34.56.78
    User john
    Port 1234

Host raspi-lan
    HostName 192.168.1.2
    User john
    Port 22

Then, you can ssh raspi-wan from outside the LAN, or ssh raspi-lan from inside the LAN without faffing about with DNS servers or editing /etc/hosts for all users, or even needing to do anything as root. If you want the name raspi to resolve differently depending upon where you are, that will probably require some shell scripting magic to detect your network and act accordingly.

share|improve this answer
1  
Thanks DopeGhoti, I am very comfortable with your solution to include a -wan and -lan postfix. My ssh however did not like the Username field (Bad configuration option). It is working fine without it. –  cfbaptista Jan 6 at 13:14
1  
I'm sorry, the correct syntax is User john, not UserName. I am correcting my answer to reflect this, and once you have thusly set your config, you can omit the username from the ssh command line. –  DopeGhoti Jan 6 at 16:44

On your computer (the connect-ing one), you can set a hostname for 12.345.67.89. Open your /etc/hosts file, and set a DNS entry :

12.345.67.89    raspi

Your machine will then transform "raspi" into "12.345.67.89" as part of a local DNS resolving process. If you use several machines, the change must be made on each and every one of them. Problem is : it requires root access to edit /etc/hosts, and you might not have it everywhere.

If you want "raspi" to be recognised automatically from anywhere, then sorry : not possible. This would require the registration of "raspi" as a domain name, which cannot happen as "raspi" has no TLD, and wouldn't depend of any DNS root server. However, you can register a domain name (let's say cfbaptista.me, and point it to your WAN IP address. With some port forwarding, you will be able to access your Raspberry Pi with :

ssh (you@)(raspi.)cfbaptista.me

(still, that's spending money for almost nothing...)

Concerning the user@ part, it depends on your login name on the different machines. If you have the same name on the connecting machine and on the remote one, then no need to specify. If not, you need to specify who you are on the remote machine.

share|improve this answer
    
note: as mentioned in the other answer, you can create host aliases in your SSH configuration, which of course does not require root. –  strugee Dec 31 '13 at 5:14
    
Indeed ! Here's a little link : collectiveidea.com/blog/archives/2011/02/04/how-to-ssh-aliases –  John WH Smith Dec 31 '13 at 15:21

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.