Short Answer:
(2) ways Here's a TESTED and WORKING specimen config from my MikroTik router that you can connect touse as a Pi on an RFC 1918 addressmodel for your own router.
chain=dstnat action=dst-nat to-addresses=192.168.0.6 to-ports=21 protocol=tcp in-interface=ether1-Gateway dst-port=60000 log=no log-prefix=""
Where:
- VPN"ether1-Gateway" is my connection to Internet
- DNAT"dst-port=60000" is an arbitrary port chosen from the "Dynamic/Private" port range.
- "to-addresses=192.168.0.6" The Pi
- "to-ports=21" The port on Pi receiving the forwarded traffic
Neither involves configuration onWARNING: Avoid "Privileged" port numbers such as "21"- use a high port in the Pi itself, but"Dynamic/Private" port range. "21" is used for FTP and now some routers are offering FTP services...
So when I SSH into the router in front of iton port 60000 as follows:
ssh [email protected] -p 60000
The router sends my traffic to the FORWARDED port "21":
192.168.0.6 on port 21
DON'T FORGET TO CHECK YOUR FIREWALL RULES allow the desired connectivity
Long Answer:
Using DNAT to connect remote-in to internal hosts that are NOT offering public services such as mail or web servers is not ideal. This topicUsing a VPN to connect to hosts on Local IPs is really one of general networking than.
Here's a comparison between the Pi itself, so I'll give you(2) methods to connect remotely to a high-level overview of what's involvedhost on an RFC 1918 address:
So once your VPN connection is established to your home/work router, your SSH connection looks like this:
ssh [email protected] -p 2160000
Note that you're reaching it on a LOCAL IP.
Configuration: In the "Short Answer" I showed you a very simple way to do a DNAT using the ROUTER's own Public IP. The following method is a bit more elegant, where each host has traffic forwarded on a Public IP dedicated to it.
Configuration: A Better DNAT Solution:
Ask your ISP for a block of Public IPs. If you ask for a /29, you're more likely to get your request approved than if you ask for a /28 (or greater). Configure one of these available addresses ( excluding the use of the router'sPublic IP of course)addresses on the router and setup a DNAT rule in the router's forwarding table to forward traffic from the public IP to the internal IP. The interface (CLI/webGUI) for accomplishing these tasks will vary between router manufacturers.
So your connection looks like:
ssh [email protected] -p 21
Compare this to the VPN example above which uses the LOCALEach host can have use a different public IP to forward traffic to it. Tidy.
Less complex to setup a DNAT than an IPsec VPN. Still requires a bit of networking knowledge, but again, a great opportunity for gaining those skills.
Firewall
Finally, ensure the FW allows traffic to the Pi on the forward table. Even if your VPN or DNAT is setup correctly, if you haven't allowed thee traffic you'll be scratching your head wondering why it's you can't connect ;-)