I often access my ubuntu machine remotely and today I will be describing how I setup my machine to access it remotely from anywhere through internet. This is not a step by step guide rather it’s a kind of walkthrough and giving overview on how I knitted each bits and pieces to get the job done. To understand this post, you will need to have basic understanding of IP addresses, basic understanding on router, port forwarding, installing/uninstalling applications on Ubuntu, webserver for hosting php files, basic understanding of cron jobs on linux.
Let’s get to the point, at first I installed xrdp on my Ubuntu box and then installed xfce4. I found this http://www.tweaking4all.com/software/linux-software/use-xrdp-remote-access-ubuntu-14-04/ link very helpful. I won’t explain how to install xrdp and xfce4. Follow the link and you will have xrdp and xfce4 installed at the end of that post.
Now the next part is to configure the router. At first enable the remote management port in your router. This instruction differ from router to router so find the instruction that suits to your case. After that forward the port 3389 to your local network IP (for e.g. 192.168.0.10). This local IP will be generally one assigned by your router to your local machine, if DHCP is enabled in your router otherwise you’ll have to manually assign static IP to your local machine. Now the most important part is to get dynamic IP. This dynamic IP is what other users on internet will use to access your computer directly. This dynamic IP varies everytime you make connections to your ISP. Each time your router connects to ISP, each time it gets a new IP assigned by ISP so knowing this IP is crucial part. this IP will be used to access your router remotely and then configure port forwarding to the local machine. I am using a php script to get the dynamic IP. I found a little PHP snippet that does the job of finding the ISP assigned IP address (WAN IP).
//Filename : ip.php
$externalContent = file_get_contents(‘http://checkip.dyndns.com/’);
preg_match(‘/Current IP Address: ([\[\]:.[0-9a-fA-F]+)</’, $externalContent, $m);
$externalIp = $m;
$date = new DateTime();
echo "Last updated :". $date->format("m/d/Y H:i:s a");
echo "WAN IP: ".$externalIp;
The output is
Last updated :10/17/2014 21:30:02 pm
WAN IP: 184.108.40.206
Save the above php snippet in ip.php and put it inside htdocs of your local webserver. In my case, I have kept ip.php file in my webserver’s test folder accessible at http://test/ip.php.
I have set cron job in my machine that runs ip.php file and saves the out on wan.txt file. I have another shell script (ftp.sh) which uploads this wan.txt file to my personal web-space. Now, this uploaded wan.txt is accessible from internet at (http://www.samundra.com.np/wan/wan.txt). I am now able to get the dynamic address assigned to my machine everytime. Below is the cronjob settings and ftp.sh contents.
echo "Uploading ip.txt via ftp"
ftp -inv $HOST << EOF user $USER $PASS put "wan.txt" "wan.txt" bye
Put the appropriate settings on HOST, USER, PASS and then save the ftp.sh and put in the same location where you have kept ip.php.
*/5 * * * * /usr/bin/wget -q -O wan.txt http://test/ip.php
*/5 * * * * /var/www/test/ftp.sh
Those were all, what I did for getting my machine to internet. I have configured various services on my machine and use them on regular basis using remote access. When you are exposing your machines to the wild you need to take precautions and use tight security measures which I haven't mentioned here. Hope this sharing helps someone out there.
Let me know your comments and feedbacks. I will try to answer your queries as soon as possible.