Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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@
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
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
    User john
    Port 1234

Host raspi-lan
    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
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 '14 at 13:14
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 '14 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

Goal: ssh raspi should work inside the LAN and on the public Internet.

To do this you need to make sure that the name resolves to the internal IP on the LAN, and the public IP from outside.

First, you should obtain a domain name such as raspi.yourdomain.com. Check out http://freedns.afraid.org/ for free domains for hobby use. Point the domain at your public IP

For the LAN, I recommend running DNSMasq. The open DD-WRT firmware tightly integrates with DNSMasq, using it for DHCP and DNS. You just have to tell it your search domain ("yourdomain.com") and it will auto-assign DNS names based on each client's requested name. To make this work, raspi's /etc/hostname should read raspi.

Once this is set up, raspi.yourdomain.com should resolve to the local IP on your LAN (just make sure you're using the local DNS on all your machines).

Now, you probably don't want to expose port 22 to the public internet, because you will get a ton of sniffer traffic. So you may have your router exposing raspi:22 as some other port, say 1234. To use the same port on both public and internal networks, you can add a port redirect rule to raspi. On Linux:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1234 -j REDIRECT --to-port 22
sudo sh -c 'iptables-save > /etc/iptables/iptables.rules'

(change eth0 to the name of your network interface as shown by ip link or ifconfig, and 1234 to your public port)

Now you can ssh -p 1234 raspi.yourdomain.com from both public and LAN.

You can add an entry to ~/.ssh/config on your client machine to shorten this to just ssh raspi, as mentioned by @DopeGhoti.

If you want to expose additional machines' SSH ports on the same public IP, just repeat the process with another DNS name and public port. Cheers!

share|improve this answer

Your Answer


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.