{"id":5563,"date":"2019-06-08T20:45:09","date_gmt":"2019-06-08T20:45:09","guid":{"rendered":"https:\/\/really.zonky.org\/?p=5563"},"modified":"2019-06-08T20:47:27","modified_gmt":"2019-06-08T20:47:27","slug":"zsh-renewed-hostname-completion","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=5563","title":{"rendered":"Zsh: Renewed Hostname Completion"},"content":{"rendered":"\n<p>Quite a while ago, I &#8220;borrowed&#8221; some inscrutable zsh magic to automatically add the contents of <em>~\/.ssh\/known_hosts<\/em> to a <em>known_hostsi<\/em> variable and used that variable to perform host name completion for certain commands. Once <em>ssh<\/em> started hashing the <em>known_hosts<\/em> file, this broke and I was busy at the time and stopped using it.<\/p>\n\n\n\n<p>Ages later, I&#8217;ve fixed it and enhanced it a bit (and arguably made it a bit simpler). Not only does it pick up host names from the <em>known_hosts<\/em> file but also adds a list from <em>lxc-ls<\/em> and adds a few static host names (with one exception, not shown). This is done by adding the following to <em>.zshrc<\/em> :-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>knownhosts=( $(sudo -b lxc-ls) )\n#       Pick up a list of hosts from lxc-ls\nknownhosts+=\"localhost\"\n#       Add static hostnames\nfor x in $(grep -v \"|\" ~\/.ssh\/known_hosts | awk '{print $1}' | awk -F, '{printf \"%s \", $1}')\ndo\n  knownhosts+=$x\ndone\n#       Pull a list of hosts out of ~\/.ssh\/known_hosts excluding the Hashed hosts.\nzstyle ':completion:*:(ssh|scp|sftp|ping|nmap):*' hosts $knownhosts\n#       Commands to use a list of known hosts with<\/code><\/pre>\n\n\n\n<p>That probably is not the most efficient code, but does have the advantage that it is relatively easy to follow.<\/p>\n\n\n\n<p>One addition is to add the option <em>HashKnownHosts no<\/em> to <em>~\/.ssh\/config<\/em>.<\/p>\n\n\n\n<p>There is of course a risk associated with disabling the hashing of host names within the <em>known_hosts<\/em> file. If your host becomes compromised, malicious code can use that file to obtain a list of hosts with which there is a trust relationship making it easier for an attacker to pivot through your network.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"463\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=695%2C463&#038;ssl=1\" alt=\"\" class=\"wp-image-3904\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?w=792&amp;ssl=1 792w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Quite a while ago, I &#8220;borrowed&#8221; some inscrutable zsh magic to automatically add the contents of ~\/.ssh\/known_hosts to a known_hostsi variable and used that variable to perform host name completion for certain commands. Once ssh started hashing the known_hosts file, this broke and I was busy at the time and stopped using it. Ages later, <a href='https:\/\/really.zonky.org\/?p=5563' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[4,209],"tags":[1866,1867,780,1212],"class_list":["post-5563","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it","tag-completion","tag-host-names","tag-ssh","tag-zsh","category-4-id","category-209-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-1rJ","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5563"}],"version-history":[{"count":2,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5563\/revisions"}],"predecessor-version":[{"id":5565,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/5563\/revisions\/5565"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}