{"id":1905,"date":"2022-05-18T16:50:40","date_gmt":"2022-05-18T14:50:40","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1905"},"modified":"2022-05-18T16:50:40","modified_gmt":"2022-05-18T14:50:40","slug":"ssh-client-config-files","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/05\/18\/ssh-client-config-files\/","title":{"rendered":"SSH Client Config Files"},"content":{"rendered":"\n<p>SSH client configuration files allow us to connect to servers with pre-configured commands. This saves time by avoiding typing each SSH command parameter when logging into a remote machine and executing commands on a remote device.<\/p>\n\n\n\n<p>If you are regularly connecting to multiple remote systems over SSH, you\u2019ll find that remembering all of the remote IP addresses, different usernames, non-standard ports, and various command-line options is difficult, if not impossible.<\/p>\n\n\n\n<h4>How to Use SSH client config files<\/h4>\n\n\n\n<p>To begin, ensure that you have SSH installed on your machine. The\u00a0<code><strong><em>.ssh<\/em><\/strong><\/code>\u00a0directory ist usualy located under the <strong><em>user\u2019s home<\/em><\/strong> directory. It \u00a0is automatically created when the user runs the\u00a0<a href=\"https:\/\/linuxize.com\/post\/ssh-command-in-linux\/\"><code>ssh<\/code><\/a>\u00a0command for the first time. If the directory doesn\u2019t exist on your system, create it using the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p ~\/.ssh &amp;&amp; chmod 700 ~\/.ssh<\/code><\/pre>\n\n\n\n<p>Next, create the SSH config file with the following command and save it in your home directory\u00a0<code>.ssh<\/code>\u00a0folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ touch \/home\/yourusername\/ .ssh\/config\n\/\/Or\ntouch ~\/.ssh\/config<\/code><\/pre>\n\n\n\n<p>This file must be readable and writable only by the user and not accessible by others:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 600 ~\/.ssh\/config \n\/\/or\nchmod 700 ~\/.ssh\/config<\/code><\/pre>\n\n\n\n<h4>Creating a connection<\/h4>\n\n\n\n<p>Typically, when connecting to a remote server via SSH, you would specify the remote user name, hostname, and port. For example, to log in as a user named\u00a0brice\u00a0to a host called\u00a0<code>dev.example.com<\/code>\u00a0on port\u00a0116\u00a0from the command line, you would type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh brice@dev.example.com -p 116<\/code><\/pre>\n\n\n\n<p>To connect to the server using the same options as provided in the command above, simply by typing\u00a0<code>ssh dev<\/code>, put the following lines to your\u00a0<code>\"<\/code><strong><em><code>~\/.ssh\/config<\/code>\u00a0<\/em>&#8221; <\/strong>file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host dev\r\n    HostName dev.example.com\r\n    User brice\r\n    Port 116\r\n<\/code><\/pre>\n\n\n\n<p><em>Now when you type\u00a0<code>ssh dev<\/code>, the ssh client will read the configuration file and use the connection details that are specified for the\u00a0<code>dev<\/code>\u00a0host:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh dev<\/code><\/pre>\n\n\n\n<p>Now let\u00b4s define multiple entries in the config file. Here is an example file showing numerous hosts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host newServer\r\n  HostName newServer.url\r\n  User adminuser\r\n  Port 2222\r\n  IdentityFile ~\/.ssh\/id_rsa.key\r\n\r\nHost anotherServer.tld\r\n  HostName anotherServer.url\r\n  User mary\r\n  Port 2222<\/code><\/pre>\n\n\n\n<p>The example file defines two Host entries in the config file and separates them using an empty line. <strong><em>It&#8217;s worth noting that the configuration file can have many entries<\/em><\/strong>. If that is the case, simply use a blank line to separate them.<\/p>\n\n\n\n<h4>Connecting<\/h4>\n\n\n\n<p>Use the specified Host identifier and run the following commands to connect to your remote server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh newServer\r\n$ ssh anotherServer.tld<\/code><\/pre>\n\n\n\n<p>The SSH client parses the config file and compares the defined Host identifier\u2019s values with the provided identifier. If they match, the configuration loads.<\/p>\n\n\n\n<p>Next, let\u2019s see what the various parameters in the config file do.<\/p>\n\n\n\n<ul><li><code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Hostname<\/strong><\/span><\/code>\u00a0specifies the actual hostname. You can also use numeric IP addresses. You can skip this if the<span class=\"has-inline-color has-vivid-cyan-blue-color\">\u00a0<code>Host identifier<\/code><\/span>\u00a0already identifies the hostname you want to connect.<\/li><li><code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Host<\/strong><\/span><\/code>\u00a0defines the host or hosts to which the configuration section applies. You can use a single asterisk (*) as a pattern to provide global defaults for all hosts.<\/li><li><code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Port<\/strong><\/span><\/code>\u00a0is where your remote SSH server is listening for connections. It defines the remote host&#8217;s connection port number, which is\u00a0<code>22<\/code>\u00a0by default.<\/li><li><code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>User<\/strong><\/span><\/code>\u00a0defines the username for the SSH connection.<\/li><li><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong><code>IdentityFile<\/code>\u00a0<\/strong><\/span>specifies the file containing the user\u2019s DSA, ECDSA or DSA authentication identity. For SSH protocol version 1, the default is\u00a0<code>~\/.ssh\/identity<\/code>. For version 2, the defaults are\u00a0<code>~\/.ssh\/id_ecdsa<\/code>\u00a0and\u00a0<code>~\/.ssh\/id_rsa<\/code>.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4>Override SSH Config File Option\u00a0<a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#override-ssh-config-file-option\">#<\/a><\/h4>\n\n\n\n<p>The ssh client reads its configuration in the following precedence order:<\/p>\n\n\n\n<ol><li><em>Options specified from the command line.<\/em><\/li><li><em>Options defined in the<strong>\u00a0<code>~\/.ssh\/config<\/code>.<\/strong><\/em><\/li><li><em>Options defined in the<strong>\u00a0<code>\/etc\/ssh\/ssh_config<\/code><\/strong>.<\/em><\/li><\/ol>\n\n\n\n<p>If you want to override a single option, you can specify it on the command line. For example, if you have the following definition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host dev\r\n    HostName dev.example.com\r\n    User brice\r\n    Port 116\r\n<\/code><\/pre>\n\n\n\n<p>and you want to use all other options but to connect as user\u00a0<code><em><strong>root<\/strong><\/em><\/code>\u00a0instead of\u00a0<em><strong>brice<\/strong><\/em>\u00a0simply specify the user on the command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -o \"User=root\" dev<\/code><\/pre>\n\n\n\n<h4>Shared SSH Config File Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Host targaryen\r\n    HostName 192.168.1.10\r\n    User daenerys\r\n    Port 7654\r\n    IdentityFile ~\/.ssh\/targaryen.key\r\n\r\nHost tyrell\r\n    HostName 192.168.10.20\r\n\r\nHost martell\r\n    HostName 192.168.10.50\r\n\r\nHost *ell\r\n    user oberyn\r\n\r\nHost * !martell\r\n    LogLevel INFO\r\n\r\nHost *\r\n    User root\r\n    Compression yes\r\n<\/code><\/pre>\n\n\n\n<p>When you type<em><span class=\"has-inline-color has-vivid-cyan-blue-color\">\u00a0<code>ssh targaryen<\/code><\/span><\/em>, the ssh client reads the file and apply the options from the first match, which is\u00a0<em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>Host targaryen<\/code>.<\/span><\/em> Then it checks the next stanzas one by one for a matching pattern. The next matching one is\u00a0<code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>Host * !martell<\/em><\/span><\/code>\u00a0(meaning all hosts except\u00a0<code>martell<\/code>), and it will apply the connection option from this stanza. The last definition\u00a0<code><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>Host *<\/em><\/span><\/code>\u00a0also matches, but the ssh client will take only the\u00a0<code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Compression<\/span><\/em><\/code>\u00a0option because the\u00a0<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">User<\/span><\/code>\u00a0option is already defined in the<em><span class=\"has-inline-color has-vivid-cyan-blue-color\">\u00a0<code>Host targaryen<\/code>\u00a0<\/span><\/em>stanza.<\/p>\n\n\n\n<p>The full list of options used when we type\u00a0<span class=\"has-inline-color has-vivid-cyan-blue-color\"><em><strong><code>ssh targaryen<\/code>\u00a0<\/strong><\/em><\/span> will be then:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HostName 192.168.1.10\r\nUser daenerys\r\nPort 7654\r\nIdentityFile ~\/.ssh\/targaryen.key\r\nLogLevel INFO\r\nCompression yes<\/code><\/pre>\n\n\n\n<p>When running\u00a0<code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">ssh tyrell<\/span><\/em><\/code>\u00a0the matching host patterns are:\u00a0<em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>Host tyrell<\/code> ,<\/span><\/em>\u00a0 <code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Host *ell<\/span><\/em><\/code>,\u00a0<em><span class=\"has-inline-color has-vivid-cyan-blue-color\"> <code>Host * !martell<\/code><\/span><\/em>\u00a0and\u00a0<code><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Host *<\/span><\/em><\/code>. The options used in this case are:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HostName 192.168.10.20\r\nUser oberyn\r\nLogLevel INFO\r\nCompression yes<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SSH client configuration files allow us to connect to servers with pre-configured commands. This saves time by avoiding typing each SSH command parameter when logging into a remote machine and executing commands on a remote device. If you are regularly connecting to multiple remote systems over SSH, you\u2019ll find that remembering all of the remote [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1910,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[212,31],"tags":[544,291,241],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1905"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1905"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"predecessor-version":[{"id":1911,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1905\/revisions\/1911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1910"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}