{"id":238,"date":"2011-07-06T09:00:16","date_gmt":"2011-07-06T16:00:16","guid":{"rendered":"http:\/\/35.225.155.113\/blog\/index.php\/2011\/07\/06\/establish_and_maintain_an_ssh_tunnel_between_linux_and_windows\/"},"modified":"2019-10-09T18:31:45","modified_gmt":"2019-10-10T01:31:45","slug":"establish-and-maintain-an-ssh-tunnel-between-linux-and-windows","status":"publish","type":"post","link":"https:\/\/www.weinstein.org\/blog\/index.php\/2011\/07\/establish-and-maintain-an-ssh-tunnel-between-linux-and-windows.html","title":{"rendered":"Establish and Maintain an SSH Tunnel between Linux and Windows"},"content":{"rendered":"<p><strong>The Situation<\/strong><\/p>\n<p>Over the years, I&#8217;ve worked in numerous computing environments and have come to appreciate heterogeneous systems. In my mind, all<br \/>\nsystem administrators should experience how different platforms solve similar problems, just as all programmers should be exposed to different programming languages.<\/p>\n<p>Of course this means being able to play well with others. Sometimes, that&#8217;s easier said than done.<\/p>\n<p>A recent project requirement stipulated being able to connect a public web server with a private database system. Not an uncommon requirement, but it did place a hurdle immediately in the way. The web application, developed with the Linux, Apache, MySQL and PHP (LAMP) stack, needed a method to connect to the private database system securely, which, for fun was not MySQL but instead Microsoft&#8217;s SQL Server.<\/p>\n<p><strong>The Problem<\/strong><br \/>\nThe initial requirement called on connecting to the SQL Server using Microsoft&#8217;s virtual private network (VPN) solution, <a href=\"http:\/\/en.wikipedia.org\/wiki\/MPPE\">Microsoft Point-to-Point Encryption<\/a> (MPPE). Not impossible, since support for MPPE on any<br \/>\nLinux distribution simply requires modifying the Linux kernel and recompiling the kernel in Linux is usually a non-issue.<\/p>\n<p>However, in this case the web application would be running on a basic <a href=\"http:\/\/en.wikipedia.org\/wiki\/Virtual_private_server\">virtual private server<\/a> (VPS) and a Linux VPS doesn&#8217;t run its own kernel. Instead Linux VPSes run on a shared kernel used by all the different virtualized servers running on the same hardware.<\/p>\n<p>Net result, no modification of the Linux kernel would be possible on the VPS.<\/p>\n<p>One alternative to this hurdle would have been to switch from a Linux VPS to a Windows VPS. This would have been technically possible since Apache, MySQL and PHP have viable Windows ports. Alas, the hosting provider in question didn&#8217;t yet offer Windows VPSes. They would shortly, but couldn&#8217;t guarantee that their Windows VPS solution would be available in time for this particular project&#8217;s deadline.<\/p>\n<p>A second alternative could have been to upgrade from a virtualized server to a dedicated server. But that would have added more computing resources than what was required. From a business perspective, the added monthly cost wasn&#8217;t justifiable. Not when a third alternative existed.<\/p>\n<p><strong>A Workable Solution<\/strong><br \/>\nVPN is one of those terms that can refer to something generic as well as something very specific<sup><a style=\"\" href=\"#_ftn1\" name=\"_ftnref1\" title=\"\">1<\/a><\/sup>. This distinction setups up alternative number three. The secure network connection requirement would remain, the implementation could simply change<sup><a style=\"\" href=\"#_ftn2\" name=\"_ftnref2\" title=\"\">2<\/a><\/sup>.<\/p>\n<p>Specifically the secure connection would be implemented via SSH instead of via MPPE.<\/p>\n<p>With SSH an encrypted tunnel through an open port in the private network&#8217;s firewall can be established. This tunnel forwards network<br \/>\ntraffic from a specified local port to a port on the remote machine, securely.<\/p>\n<p>Most Linux distributions these days install <a href=\"http:\/\/openssh.org\/\">OpenSSH<\/a> as part of their base system install. OpenSSH is a free and open version of the SSH protocol and includes client and server software. For those distributions that<br \/>\ndon&#8217;t install it by default installing OpenSSH is usually a trivial matter via the distribution&#8217;s package manager.<\/p>\n<p>Windows, on the other-hand, has no such base installation of<br \/>\nan SSH implementation. There are a number of free software versions for Windows. For the case at hand, <a href=\"http:\/\/www.freesshd.com\/\">freeSSHD<\/a> was selected to provide a free, open<br \/>\nsource version of the SSH server software.<\/p>\n<p>Configuring <i>freeSSHD<\/i> to enable tunneling requires the<br \/>\nfollowing steps:<\/p>\n<ol>\n<li>Click on the &#8220;Tunneling&#8221; tab<\/li>\n<li>Check to enable port forwarding and apply the<br \/>\nchange<\/li>\n<li>Click on the &#8220;Users&#8221; tab<\/li>\n<li>Create or edit a user and enable tunnel access<\/li>\n<\/ol>\n<p>Once the firewall has been configured to allow SSH traffic<br \/>\non port 22, establishing the tunnel from the Linux client to the Windows server is as simple as typing the following at the Linux command-line:<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\nssh -f -N -L 127.0.0.1:1433:192.168.1.2:1433 username@example.org<\/div>\n<p>In which <i>ssh <\/i>will create and send to the background a ssh<br \/>\ntunnel (-f option) without executing any remote commands (-N option) that begins at the localhost port 1433 (127.0.0.1:1433) terminates at the remote address and port (192.168.1.2:1433) and authenticates using the remote username at the remote location (the public IP address or domain name for the private network).<\/p>\n<p><strong>But Wait There&#8217;s More<\/strong><br \/>\nThere is however a minor problem with this SSH tunnel. As<br \/>\ndescribed, the establishment of the SSH tunnel is an interactive process. The command needs to be executed and the password for the user provided for authentication. In most cases a simple shell script, executed by cron would solve this minor issue. However, for the sake of security OpenSSH doesn&#8217;t provide a command-line option for providing passwords.<\/p>\n<p>This authentication step can be managed in one of two ways.<br \/>\nOne is the use of a key management program such as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ssh-agent\">ssh-agent<\/a>. The second, more common option is to create a passphrase-less key.<\/p>\n<p>The first step in creating a passphrase-less key is to first<br \/>\ngenerate a private\/public key pair&gt;sup&gt;<a style=\"\" href=\"#_ftn3\" name=\"_ftnref3\" title=\"\">3<\/a>.<br \/>\nIn Linux this is done by issuing the command:<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\nssh-keygen -t rsa<\/div>\n<p>Which generates a private\/public key pair based on either<br \/>\nthe <a href=\"http:\/\/en.wikipedia.org\/wiki\/RSA\">RSA<\/a> or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Digital_Signature_Algorithm\">DSA<\/a> encryption algorithm, depending on what is provided in the command-line option.<\/p>\n<p>When prompted to enter a passphrase for the securing of the<br \/>\nprivate key simply press enter. To confirm the empty passphrase simply press enter again.<\/p>\n<p>The next step, after copying the public key onto the Windows<br \/>\nserver, is to enable the use of the public key for authentication. In <i>freeSSHD<\/i>the steps are:<\/p>\n<ol>\n<li>Click on the &#8220;Users&#8221; tab<\/li>\n<li>Select a user and click on &#8220;Change&#8221;<\/li>\n<li>Select &#8220;Public Key&#8221; from the &#8220;Authorization&#8221; drop-down<\/li>\n<li>Click on &#8220;OK&#8221; to save changes to users<\/li>\n<li>Next click on the &#8220;Authentication&#8221; tab<\/li>\n<li>Using the browse button, select the directory with the users public key are kept<\/li>\n<li>Enable public-key authentication by choosing the &#8220;Allowed&#8221; button under &#8220;Public-Key Authentication&#8221;<\/li>\n<li>Click on &#8220;OK&#8221; to save the changes to authentication<\/li>\n<\/ol>\n<p>With the passphrase-less keys in place, the last step is to<br \/>\nautomate the tunnel itself. In this case, instead of a shell script, I opted to use program called <a href=\"http:\/\/www.harding.motd.ca\/autossh\/\">autossh<\/a>.<\/p>\n<p><i>autossh<\/i> is a program that can start a copy of ssh and<br \/>\nmonitor the connection, restarting it when necessary. All <i>autossh <\/i>needs to know is what local port to monitor, so our one-time initial startup of ssh tunnel looks similar to the previous example, but with autossh and the addition of the -M option<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\nautossh -M 1433 -f -N -L 127.0.0.1:1433:192.168.1.2:1433<br \/>\nusername@example.org<\/div>\n<div>\n<hr width=\"33%\" size=\"1\" align=\"left\">\n<div style=\"\" id=\"ftn1\">\n<p class=\"MsoFootnoteText\"><a style=\"\" href=\"#_ftnref1\" name=\"_ftn1\" title=\"\"><span class=\"MsoFootnoteReference\"><span style=\"\"><!--[if !supportFootnotes]--><span class=\"MsoFootnoteReference\"><span style=\"font-size: 10pt; line-height: 115%; font-family: &quot;Calibri&quot;,&quot;sans-serif&quot;;\">[1]<\/span><\/span><!--[endif]--><\/span><\/span><\/a><br \/>\nThis means alas, it is also one of those terms that can cause confusion, especially between technical and non-technical people, if not defined at the outset.<\/p>\n<\/div>\n<div style=\"\" id=\"ftn2\">\n<p class=\"MsoFootnoteText\"><a style=\"\" href=\"#_ftnref2\" name=\"_ftn2\" title=\"\"><span class=\"MsoFootnoteReference\"><span style=\"\"><!--[if !supportFootnotes]--><span class=\"MsoFootnoteReference\"><span style=\"font-size: 10pt; line-height: 115%; font-family: &quot;Calibri&quot;,&quot;sans-serif&quot;;\">[2]<\/span><\/span><!--[endif]--><\/span><\/span><\/a> This is one of those places where knowledge of different solutions solving a similar problem becomes handy.<\/p>\n<\/div>\n<div style=\"\" id=\"ftn3\">\n<p class=\"MsoFootnoteText\"><a style=\"\" href=\"#_ftnref3\" name=\"_ftn3\" title=\"\"><span class=\"MsoFootnoteReference\"><span style=\"\"><!--[if !supportFootnotes]--><span class=\"MsoFootnoteReference\"><span style=\"font-size: 10pt; line-height: 115%; font-family: &quot;Calibri&quot;,&quot;sans-serif&quot;;\">[3]<\/span><\/span><!--[endif]--><\/span><\/span><\/a><br \/>\nFor user authentication SSH can either be password-based or key-based. In key-based authentication, SSH uses public-key cryptography where the public key is distributed to identify the owner of the matching private key. The passphase is in this case is used to authenticate access to the private key.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Situation Over the years, I&#8217;ve worked in numerous computing environments and have come to appreciate heterogeneous systems. In my mind, all system administrators should experience how different platforms solve similar problems, just as all programmers should be exposed to different programming languages. Of course this means being able to play well with others. Sometimes, [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,92,82],"tags":[12,285,6,181,334,43,331,335,333,332,182],"_links":{"self":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/238"}],"collection":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":1,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/238\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}