{"id":512,"date":"2013-03-10T16:43:52","date_gmt":"2013-03-10T15:43:52","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=512"},"modified":"2016-10-11T11:01:32","modified_gmt":"2016-10-11T09:01:32","slug":"srv-ssh","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=512","title":{"rendered":"SSH Survival Guide"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Authentification_using_publicprivate_key\"><span class=\"toc_number toc_depth_1\">1<\/span> Authentification using public\/private key<\/a><ul><li><a href=\"#Generate_publicprivate_key\"><span class=\"toc_number toc_depth_2\">1.1<\/span> Generate public\/private key<\/a><\/li><li><a href=\"#Export_the_public_key\"><span class=\"toc_number toc_depth_2\">1.2<\/span> Export the public key<\/a><\/li><li><a href=\"#Test\"><span class=\"toc_number toc_depth_2\">1.3<\/span> Test<\/a><\/li><\/ul><\/li><li><a href=\"#Tunneling\"><span class=\"toc_number toc_depth_1\">2<\/span> Tunneling<\/a><ul><li><a href=\"#Example_1\"><span class=\"toc_number toc_depth_2\">2.1<\/span> Example 1<\/a><\/li><li><a href=\"#Example_2\"><span class=\"toc_number toc_depth_2\">2.2<\/span> Example 2<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Authentification_using_publicprivate_key\">Authentification using public\/private key<\/span><\/h1>\n<p>We can automatically login via ssh using keypair. So we will describe how to generate public\/private key and install it in the client and the server.<\/p>\n<h2><span id=\"Generate_publicprivate_key\">Generate public\/private key<\/span><\/h2>\n<p>First, the client must generate the couple public\/private key:<\/p>\n<pre lang=\"bash\">\r\nssh-keygen -t rsa\r\n<\/pre>\n<p>Two files have been create:<\/p>\n<ul>\n<li>~\/.ssh\/id_rsa : the private key<\/li>\n<li>~\/.ssh\/id_rsa_pub: the public key<\/li>\n<\/ul>\n<h2><span id=\"Export_the_public_key\">Export the public key<\/span><\/h2>\n<p>Then, we have to export the client public key to the server:<\/p>\n<pre lang=\"bash\">\r\ncat ~\/.ssh\/id_rsa.pub | ssh user@myserver \"cat - >>~\/.ssh\/authorized_keys\"\r\n<\/pre>\n<p>or you can also use this easiest command:<\/p>\n<pre lang=\"bash\">\r\nssh-copy-id -i ~\/.ssh\/id_rsa.pub user@myserver\r\n<\/pre>\n<h2><span id=\"Test\">Test<\/span><\/h2>\n<p>Finally, we have to test if it works (we have to login without authentication prompt):<\/p>\n<pre lang=\"bash\">\r\nssh user@myserver\r\n<\/pre>\n<h1><span id=\"Tunneling\">Tunneling<\/span><\/h1>\n<h2><span id=\"Example_1\">Example 1<\/span><\/h2>\n<pre lang=\"bash\">\r\nssh -f user@myserver -L localPort:myserver:remotePort -N\r\n<\/pre>\n<p>Then, use this adress to access to the service: localhost:localPort<\/p>\n<h2><span id=\"Example_2\">Example 2<\/span><\/h2>\n<p>First, create ~\/.ssh\/config file with the following content<\/p>\n<pre lang=\"bash\">\r\n# A name to this configuration\r\nHost myHost\r\n# Your server hostname\r\nHostName  myserver\r\n# Your server port\r\nPort remotePort\r\n# Your server user\r\nUser user\r\n\r\n########################\r\n# Here begin the rules:\r\n########################\r\n\r\n# We will connect to all resources on a subnet throw the server named 'myserver'.\r\n\r\n# Server1\r\nLocalForward localPort1 ipAddress1:Port1\r\n\r\n# Server2\r\nLocalForward localPort2 ipAddress1:Port2\r\n\r\n# Example1\r\nLocalForward 1597 192.168.0.8:80\r\n\r\n# Example2\r\nLocalForward 1598 192.168.0.9:21\r\n\r\n<\/pre>\n<p>Then, connect like this:<\/p>\n<pre lang=\"bash\">\r\nssh -N myHost\r\n<\/pre>\n<p>Now you will find be able to use to subnet resources like this: localhost:localPortX.<br \/>\nFor example:<br \/>\n &#8211; if you want to look the website on the 192.168.0.8 server : http:\/\/localhost:1597.<br \/>\n &#8211; if you want to connect to the ftp on the 192.168.0.9 server: ftp:\/\/localhost:1598.<\/p>\n<p>Note, you can not access to the resources that does not exists on your config file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Authentification using public\/private key1.1 Generate public\/private key1.2 Export the public key1.3 Test2 Tunneling2.1 Example 12.2 Example 2 Authentification using public\/private key We can automatically login via ssh using keypair. So we will describe how to generate public\/private key and install it in the client and the server. Generate public\/private key First, the client must [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":492,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-512","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=512"}],"version-history":[{"count":9,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/512\/revisions"}],"predecessor-version":[{"id":1748,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/512\/revisions\/1748"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/492"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}