{"id":6803,"date":"2019-08-06T01:06:44","date_gmt":"2019-08-05T20:36:44","guid":{"rendered":"http:\/\/blog.mahdi.jafari.siavoshani.ir\/?page_id=6803"},"modified":"2019-08-06T02:15:43","modified_gmt":"2019-08-05T21:45:43","slug":"connectivity-tips","status":"publish","type":"page","link":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/?page_id=6803","title":{"rendered":"Connectivity Tips"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">SSH passwordless login<\/h3>\n\n\n\n<p>Let us assume that we want to connect to an SSH server from a Linux client without entering your password each time. We use &#8220;cli_ip&#8221; and &#8220;srv_ip&#8221; to represent the <g class=\"gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"5\" data-gr-id=\"5\">ip<\/g> addresses of the client and server. The steps can be summarised as follows.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Create authentication SSH-Kegen keys on the client. <\/p>\n\n\n\n<p>On your client machine run the command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nssh-keygen -t rsa -b 4096\n<\/pre><\/div>\n\n\n<p> Step 2: Create .ssh directory on the server.<\/p>\n\n\n\n<p>First, SSH to the server and check if the directory &#8220;.ssh&#8221; exits on your home directory. Since &#8220;.ssh&#8221; is a hidden directory, you can check its existence by the following command in the terminal<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nls -la\n<\/pre><\/div>\n\n\n<p>If the directory does not exist, create it by<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmkdir .ssh\n<\/pre><\/div>\n\n\n<p><strong>Step 3:<\/strong> Upload generated public keys from the client to the server<\/p>\n\n\n\n<p>Run the following command to upload the generated public key in Step 1 from the client to the server<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncat .ssh\/id_rsa.pub | ssh your_username@srv_ip &#039;cat &gt;&gt; .ssh\/authorized_keys&#039;\n<\/pre><\/div>\n\n\n<p><strong>Step 4:<\/strong> Set directory permissions on the server<\/p>\n\n\n\n<p>Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file. First, SSH to the server and then run the following commands<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nchmod 700 .ssh \nchmod 640 .ssh\/authorized_keys\n<\/pre><\/div>\n\n\n<p><strong>Step 5:<\/strong> Login from client to server without password<\/p>\n\n\n\n<p>Now you should be able to remotely login to the server srv_ip from client cli_ip with SSH without entering your password<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nssh your_username@srv_ip \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Persistent SSH Connection using autossh<\/h3>\n\n\n\n<p> &#8220;Autossh&#8221; is a program that starts a copy of ssh, monitors it, and restarts the connection if necessary.  To install autossh on Debian\/Ubuntu you can run<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo apt-get install autossh\n<\/pre><\/div>\n\n\n<p>and to install it on CentOS\/Fedora\/RHEL you can run <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo yum install autossh \n<\/pre><\/div>\n\n\n<p>It can also be installed on OSX by the following command<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbrew install autossh\n<\/pre><\/div>\n\n\n<p>To run <g class=\"gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"13\" data-gr-id=\"13\">autossh<\/g>, a sample command can be as follows<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nautossh -M 0 -o &quot;ServerAliveInterval 30&quot; -o &quot;ServerAliveCountMax 3&quot; -o &quot;ExitOnForwardFailure yes&quot; -R 8080:127.0.0.1:80 -R 2222:10.0.0.100:22 -D *:3128 rmt_s rv_ip\n<\/pre><\/div>\n\n\n<p>The option &#8220;-M port[:echo_port]&#8221; specifies the base monitoring port to use. Without the echo port, this port and the port immediately above it (port + 1) should be something nothing else is using. <g class=\"gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"12\" data-gr-id=\"12\">Autossh<\/g> will send test data on the base monitoring port and receive it back on the port above. For example, if you specify &#8220;-M 20000&#8221;, <g class=\"gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"13\" data-gr-id=\"13\">autossh<\/g> will set up forwards so that it can send data on port 20000 and receive it back <g class=\"gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace\" id=\"9\" data-gr-id=\"9\">on<\/g> 20001. Setting the monitor port to 0 turns the monitoring function off, and <g class=\"gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling\" id=\"14\" data-gr-id=\"14\">autossh<\/g> will only restart ssh upon ssh&#8217;s exit. For example, if you are using a recent version of OpenSSH, you may wish to explore using the &#8220;ServerAliveInterval&#8221; and &#8220;ServerAliveCountMax&#8221; options to have the SSH client exit if it finds itself no longer connected to the server. In many ways, this may be a better solution than the monitoring port.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using SSHuttle to tunnel the traffic<\/h3>\n\n\n\n<p>&#8220;<g class=\"gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"9\" data-gr-id=\"9\">sshuttle<\/g>&#8221; is a transparent proxy server that works as a poor man&#8217;s VPN over ssh. You don&#8217;t need an admin account on your remote system. It supports DNS tunneling and works with Linux and <g class=\"gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"10\" data-gr-id=\"10\">MacOS<\/g> platforms. <\/p>\n\n\n\n<p><g class=\"gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"4\" data-gr-id=\"4\">sshuttle<\/g> is one of the simplest, yet very powerful way to setup VPN on any network to which you have SSH access. The beauty of this application is you need root access in your local system, but don&#8217;t need any administrative access on your remote side. <\/p>\n\n\n\n<p>One can install sshuttle by running<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napt-get install sshuttle\n<\/pre><\/div>\n\n\n<p>Then, to use it as a VPN on your local machine to forward all traffic to the remote server, one can run:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsshuttle -r username@sshserver 0.0.0.0\/0\n<\/pre><\/div>\n\n\n<p>If we would also like our DNS queries to be proxied through the DNS server of the server we are connected to, the following command can be run:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsshuttle --dns -r username@sshserver 0\/0\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>SSH passwordless login Let us assume that we want to connect to an SSH server from a Linux client without entering your password each time. We use &#8220;cli_ip&#8221; and &#8220;srv_ip&#8221; to represent the ip addresses of the client and server. The steps can be summarised as follows. Step 1: Create authentication SSH-Kegen keys on the <a href='https:\/\/blog.mahdi.jafari.siavoshani.ir\/?page_id=6803' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":6788,"menu_order":1,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6803","page","type-page","status-publish","hentry","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/pages\/6803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6803"}],"version-history":[{"count":19,"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/pages\/6803\/revisions"}],"predecessor-version":[{"id":6826,"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/pages\/6803\/revisions\/6826"}],"up":[{"embeddable":true,"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=\/wp\/v2\/pages\/6788"}],"wp:attachment":[{"href":"https:\/\/blog.mahdi.jafari.siavoshani.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}