Ionică Bizău

Accessing My Home Computer Remotely

I have a powerful ASUS machine which I use when I'm at home. I guess it was designed for gaming. It's quite useful for any tasks, but I often use it for expensive tasks using more resources (e.g. training neural networks). It got a fast enough wired internet connection, as well! :rocket:

When travelling, I do not have physical access to my home computer. However, I do want to access it sometimes (when I work on projects requiring lots of computations). :airplane:

The solution I ended with is to connect to it via SSH. :lock:

So, using my MacBook, I can simply run ssh -A ionicabizau@<public-ip> -p <port> and land in my home. There are a couple of problems, tho. Here is how I did it! :sparkles:

Port Forwarding

Using ifconfig we can see what IP the laptop got on the network:

$ ifconfig | grep 192
        inet 192.168.2.xxx  netmask 255.255.255.0  broadcast 192.168.2.255

(the xxx can be 100, 101 etc).

I opened 192.168.2.1 in the browser (accessing the router settings) and set up port forwarding on this port range: 2042-5042 and using the IP I got in the previous command.

Then, I changed my laptop SSH server port and made it listen on 4242 (which, indeed, is in the range mentioned above).

Restarted the router and then I did a curl ipinfo.io (which outputs the public IP information) and connected successfully to my machine, from my machine, but using the public IP.

Great! There are a couple of issues, tho!

If the laptop reconnects to the router, it may get a different IP on the network. Also, if the router reconnects to the internet, usually it happens to get a different public IP. :boom:

Same IP on the network

By running ifconfig I found out that the wired connection has the name enp5s0.

Then, I modified the /etc/network/interfaces file like this (following a couple of articles from the internet):

auto lo
iface lo inet loopback

# Set up a static ip on the network
auto enp5s0
iface enp5s0 inet static
address 192.168.2.142
netmask 255.255.255.0
gateway 192.168.2.1
dns-nameservers 8.8.8.8 192.168.1.1

Reconnected to the router, and I noticed 192.168.2.142 in the ifconfig output. Rebooted, and the ip didn't change.

Now, I got back in the router settings and exposed the ports 2042-5042 on 192.168.2.142 to the internet.

But the public IP may change...

I don't have a static IP. If I'm not wrong, one has to pay to the internet provider to get a static IP. I don't care if it's going to change, as long I know what new public IP it got.

I made a small tool which pushes the ip information in a GitHub repository: machine-ip. It uses ipinfo.io to get the public ip information.

I created a GitHub repository storing the ip information of my home machine. This is updated every 10 minutes automatically

IpInfo allows as to make 1000 daily requests for free. That is around 41 requests an hour (1000 / 24 is 41.666...). Therefore the 10-minute update, which is good enough.

Running this in a cron job

I made another script which is executed in a cron job.

echo "Adding the ssh key"
ssh-add /home/testing/.ssh/id_rsa
echo "Changing directory"
cd /johnnysapps/notebook
echo "Getting the ip"
date > last_updated.txt
machine-ip

I ran sudo crontab -e -u testing and created my first cron job I've evern written:

SHELL=/bin/sh
PATH=/home/testing/.nvm/versions/node/v6.7.0/bin:/home/testing/bin:/home/testing/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

# Min Hour Day Month Weekday Command
*/10  *    *   *     *       /johnnysapps/ip

*/10 tells the system to execute my script every 10 minutes.


So, apparently, it's working! :tada: Using my SSH keys I can connect to my home computer. In case the electricity goes down, I do hope it will not be down for more than 1-2 hours (which is supported by the internal battery).

When the electricity is back, the modem, the router and my laptop are going to connect back to the internet and my cron job is going to push the new IP data in my GitHub account.

Sweet! Now I can :airplane: :rocket:!

Have feedback on this article? Let @IonicaBizau know on Twitter.

Have any questions or suggestions? Contact me. Found a bug? Report it now