<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>ngetchell.com - Posts tagged with &quot;Linux&quot;</title><description>All blog posts tagged with &quot;Linux&quot;</description><link>https://ngetchell.com/</link><item><title>My Favorite SSH Tricks</title><link>https://ngetchell.com/blog/my-favorite-ssh-tricks/</link><guid isPermaLink="true">https://ngetchell.com/blog/my-favorite-ssh-tricks/</guid><description>Explore the ssh_config to make yourself more productive</description><pubDate>Tue, 10 Oct 2023 11:29:56 GMT</pubDate><content:encoded>
&lt;p&gt;After years and years of administering Linux servers on the web, I&apos;ve picked up a few tricks.
I&apos;ve also read many books on SSH including &lt;a href=&quot;https://mwl.io/nonfiction/tools#ssh&quot;&gt;SSH Mastery&lt;/a&gt; by Matthew W Lucas and &lt;a href=&quot;https://www.amazon.com/SSH-Secure-Shell-Definitive-Guide/dp/0596008953/ref=sr_1_5?dchild=1&amp;amp;keywords=ssh&amp;amp;qid=1600553908&amp;amp;sr=8-5&quot;&gt;SSH, The Secure Shell&lt;/a&gt; from O&apos;Reilly.
These are the tricks I&apos;ve integrated into my day-to-day life.&lt;/p&gt;



&lt;h2&gt;Import Authorized Keys from GitHub&lt;/h2&gt;



&lt;p&gt;Using Git on GitHub without uploading an SSH key is very frustrating and I don&apos;t recommend it.
Since you push your public keys up to GitHub, why not use those when provisioning a server?&lt;/p&gt;



&lt;p&gt;Note: Automating this could add an attack vector if someone were to take over your GitHub account.
The threat actor could add their ssh key to your account and then get access to whatever servers import identities automatically.
Make sure to inspect the ~/.ssh/authorized_keys file to confirm no odd public key is listed.
Turn on &lt;a href=&quot;https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/configuring-two-factor-authentication&quot;&gt;MFA on your GitHub account&lt;/a&gt; to reduce the risk.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;# Install on Ubuntu/Debian&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;apt&lt;/span&gt;&lt;span&gt; install&lt;/span&gt;&lt;span&gt; ssh-import-id&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;# OR&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;# Install using pip&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;pip&lt;/span&gt;&lt;span&gt; install&lt;/span&gt;&lt;span&gt; ssh-import-id&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;ssh-import-id-gh&lt;/span&gt;&lt;span&gt; &amp;lt;&lt;/span&gt;&lt;span&gt;usernam&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;Using a Jumphost&lt;/h2&gt;



&lt;p&gt;Jump hosts allow you to connect to a server, THRU another server, all over SSH.
This is great for bastion hosts or accessing a home server thru your home router.
No need to add additional port forwards to reach the secondary server via SSH.&lt;/p&gt;



&lt;h3&gt;From the command line&lt;/h3&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;ssh&lt;/span&gt;&lt;span&gt; -J&lt;/span&gt;&lt;span&gt; jumper@jumphost.internal&lt;/span&gt;&lt;span&gt; user@dest.host.domain&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;SSH Config File&lt;/h3&gt;



&lt;p&gt;Who wants to remember the -J parameter for every connection?
Use your SSH Config file to remember your settings.
Edit ~/.ssh/config to configure for your user account.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;Host dest-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  User hunter&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  Hostname dest-host.domain&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  ProxyJump jump-srv&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  IdentityFile ~/.ssh/dest-host.pem&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;Host jump-srv&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  User other_user&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  Hostname jump-srv&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  IdentityFile ~/.ssh/jump-srv&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You can now access dest-host thru jump-srv.
This is useful if you have one host accessible on the web but would like to access a host inside of your network.
Network administrators also configure a jumphost or a bastion host to concentrate access thru a hardened box exposed to the internet.&lt;/p&gt;



&lt;p&gt;With this method, you can also see how we specified a different key file for each host.
It helps keep your identities separate.
I keep multiple key files for work and home and for different purposes depending on what I&apos;m trying to connect to.&lt;/p&gt;



&lt;h2&gt;Canonical Domains and Hosts&lt;/h2&gt;



&lt;p&gt;Canonical domains and hosts allow you to use the hostname of a server but really connect via the fully-qualified domain name.
This is great for confirming you are hitting the server you expect to access.
It also helps with host key matching.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;CanonicalizeHostname yes&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;CanonicalDomains ngetchell.com&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;Never Prompt for a Passwoord&lt;/h2&gt;



&lt;p&gt;While it is recommended to use SSH keys over passwords, the below host will force SSH to try your keys first.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;Host dmz-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  Host dmz-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  User admin_account&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  IdentityFile ~/.ssh/admin_account&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;Host *&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  PreferredAuthentications publickey&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;Choose a Key by Domain&lt;/h3&gt;



&lt;p&gt;If you&apos;re a consultant, being forced to sign into multiple domains, keeping track of each user, key, ports, or hostnames can be difficult.
Consider using the following ssh user configuration to create domain specific configurations.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;Host *.companyA.com&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;   User consultant&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;   IdentityFile ~/.ssh/companyA&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;   Port 2222&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;Host *.companyB.net&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    User vendor&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    IdentityFile ~/.ssh/companyB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Using SSH to connect to host1.companyA.com will now automatically connect on port 2222, as user consultant, with your companyA ssh key.&lt;/p&gt;



&lt;h2&gt;Tunnels&lt;/h2&gt;



&lt;p&gt;Tunnels via SSH are an easy way to share ports, apps, and services between two hosts.&lt;/p&gt;



&lt;h3&gt;Local Forward&lt;/h3&gt;



&lt;p&gt;Local forwards allow a client to connect to a server, then map a port from the server back down to the client.
It is a simple way to bridge a mysql connection for use with MySQL Workbench on a client computer.
You could also host a web server on a remote hosts then bring that bound port down to your local host.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;# ssh -L &amp;lt;local port&amp;gt;:&amp;lt;remote hostname&amp;gt;:&amp;lt;remote port&amp;gt; &amp;lt;remote hostname&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;ssh&lt;/span&gt;&lt;span&gt; -L&lt;/span&gt;&lt;span&gt; 5000:remote-host:443&lt;/span&gt;&lt;span&gt; remote-host&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;Host remote-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  LocalForward 5000 localhost:4000&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;Remote Forward&lt;/h3&gt;



&lt;p&gt;Remote port fowards do the opposite. Once connected, SSH shares a client port, app, or service with a server.&lt;/p&gt;



&lt;p&gt;In the past I&apos;ve used this to establish SSH connections to two servers behind a firewall I had no control over.
Since the client starts the connection, most firewalls allow it.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;# ssh -R &amp;lt;remote port&amp;gt;:&amp;lt;local hostname&amp;gt;:&amp;lt;local port to foward&amp;gt; &amp;lt;hostname&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;ssh&lt;/span&gt;&lt;span&gt; -R&lt;/span&gt;&lt;span&gt; 5000:localhost:4000&lt;/span&gt;&lt;span&gt; remote-host&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;Host remote-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  RemoteForward 5000 localhost:4000&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;Dynamic or SOCKS Forward&lt;/h3&gt;



&lt;p&gt;A dynamic forward lets you route various types of traffic through a local port and is primarily used as a SOCKS proxy in the browser.
If you have SSH access that allows for forwarding you can route your browser traffic through it as an almost simple VPN.
Add-ons like &lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/proxy-toggle/&quot;&gt;Proxy Toggle&lt;/a&gt; for Firefox lets you configure a SOCKS5 proxy that you can quickly turn off an on as needed.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;# ssh -D &amp;lt;local port&amp;gt; remote-host&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;ssh&lt;/span&gt;&lt;span&gt; -D&lt;/span&gt;&lt;span&gt; 55555&lt;/span&gt;&lt;span&gt; remote-host&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</content:encoded><category>Linux</category><category>SSH</category></item></channel></rss>