{"id":4706,"date":"2017-11-29T22:27:48","date_gmt":"2017-11-29T22:27:48","guid":{"rendered":"https:\/\/islascruz.org\/blog\/?p=4706"},"modified":"2017-11-29T22:27:48","modified_gmt":"2017-11-29T22:27:48","slug":"repost-speed-ssh-controlmaster","status":"publish","type":"post","link":"https:\/\/islascruz.org\/blog\/2017\/11\/29\/repost-speed-ssh-controlmaster\/","title":{"rendered":"REPOST: Speed up SSH with ControlMaster"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4708 size-full\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/uploads\/2017\/11\/1817_ssh.jpg\" alt=\"\" width=\"510\" height=\"713\" srcset=\"https:\/\/islascruz.org\/blog\/wp-content\/uploads\/2017\/11\/1817_ssh.jpg 510w, https:\/\/islascruz.org\/blog\/wp-content\/uploads\/2017\/11\/1817_ssh-215x300.jpg 215w, https:\/\/islascruz.org\/blog\/wp-content\/uploads\/2017\/11\/1817_ssh-365x510.jpg 365w, https:\/\/islascruz.org\/blog\/wp-content\/uploads\/2017\/11\/1817_ssh-286x400.jpg 286w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/p>\n<p>NOTE: This \u00a0is a re-post from the old blog which you can find <a href=\"http:\/\/islascruz.org\/html\/index.php\/blog\/show\/Speed_up_SSH_with_ControlMaster1.html\" target=\"_blank\" rel=\"noopener\">here<\/a><\/p>\n<p>Quick post, this is an ancient method to speed up ssh connections, I&#8217;m just leaving it here.<\/p>\n<p>When we use SSH mostly all the times we create a new connection, even if we are connecting with the same host at the same port with the same user. I&#8217;ve been using SSH for a long time and to me this was fine, at the end the connection is &#8220;pretty&#8221; fast and the delay time creating the connection is just ok.<\/p>\n<p>The problem is when you are editing a file that is in the server, whenever I can I use ssh and with that use vim to edit remote files using &#8220;scp&#8221;, with this I can test the file in the remote server without worrying to sync the file (rsync\/scp) manually. At the end I just write back the file in my hard drive if needed.<\/p>\n<p>This is kind of\u2026 annoying, vim+scp creates a new connection every time you want to save the file, and it mean a lot of time when editing a file, more if you are like me, saving the freaking file every time I stop writing. So, how can I improve that?.<\/p>\n<p>Well, it turns out its pretty simple. ssh can create a socket (in a predefined place) when connected to a server, with this socket any other ssh instance for the same server+user+port can use this socket and avoid the connection delay. To enable this just add this lines in your ssh config:<\/p>\n<p>&nbsp;<\/p>\n<pre>Host *\n     ControlMaster auto\n     ControlPath ~\/.ssh\/socket\/ssh_mux_%h_%p_%r\n<\/pre>\n<p>Just make sure that <code>~\/.ssh\/socket\/<\/code> \u00a0exists. This will create a socket like <code>~\/.ssh\/socket\/ssh_mux_localhost_22_markuz<\/code> \u00a0and all new connections will use it instead of creating a new connection. Of course, there are some downsides, since the first connection is the only one that is <em>connected<\/em>, if you loose that connection you&#8217;ll disconnect all other ssh instances.You&#8217;ll probably do this. So, the way I&#8217;ve managed to fix it. Well, it&#8217;s kind of simple and of course, it is not magic, just create a master connection whenever you can:<\/p>\n<pre>ssh -MNn user@host\n<\/pre>\n<p>Put this somewhere to create it magically (let&#8217;s say a script that run in the background), with this you&#8217;ll have that ssh connection open, you&#8217;ll never have it in the terminal and you&#8217;ll not close it until you &#8220;kill&#8221; that ssh connection.<\/p>\n<p>So, the fix is more like a hack, but works, and helps a lot if you are using a lot of connections to a server.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4706\" class=\"pvc_stats all  \" data-element-id=\"4706\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>NOTE: This \u00a0is a re-post from the old blog which you can find here Quick post, this is an ancient method to speed up ssh connections, I&#8217;m just leaving it here. When we use SSH mostly all the times we create a new connection, even if we are connecting with the same host at the [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4706\" class=\"pvc_stats all  \" data-element-id=\"4706\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":4708,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[239],"tags":[],"class_list":["post-4706","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"a3_pvc":{"activated":true,"total_views":7435,"today_views":0},"brizy_media":[],"_links":{"self":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/comments?post=4706"}],"version-history":[{"count":3,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4706\/revisions"}],"predecessor-version":[{"id":4711,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4706\/revisions\/4711"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media\/4708"}],"wp:attachment":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media?parent=4706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/categories?post=4706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/tags?post=4706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}