{"id":7726,"date":"2019-01-09T12:04:23","date_gmt":"2019-01-09T12:04:23","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=7726"},"modified":"2019-01-12T09:55:54","modified_gmt":"2019-01-12T09:55:54","slug":"linux-today-using-the-ssh-config-file","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/09\/linux-today-using-the-ssh-config-file\/","title":{"rendered":"Linux Today &#8211; Using the SSH Config File"},"content":{"rendered":"<div class=\"mx-auto lg:ml-0 lg:mr-auto lg:pr-6 xl:mx-0 max-w-lg mb-6 xl:max-w-full xl:pr-8\">\n<div class=\"post-header\"><span style=\"font-size: 1rem;\">If you are regularly connecting to multiple remote systems over SSH on a daily basis, 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.<\/span><\/div>\n<\/div>\n<p>One option would be to\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-create-bash-aliases\/\">create a bash alias<\/a>\u00a0for each remote server connection. However, there is another, much better and more simpler solution to this problem. OpenSSH allows you to set up per-user configuration file where you can store different SSH options for each remote machine you connect to.<\/p>\n<p>This guide covers the basics of the SSH client configuration file and explains some of the most common configuration options.<\/p>\n<h2 id=\"prerequisites\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#prerequisites\" data-scroll=\"\">Prerequisites<\/a><\/h2>\n<p>We are assuming that you are using a Linux or a macOS system with OpenSSH client installed.<\/p>\n<h2 id=\"ssh-config-file-location\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#ssh-config-file-location\" data-scroll=\"\">SSH Config File Location<\/a><\/h2>\n<p>OpenSSH client-side configuration file is named\u00a0<code>config<\/code>\u00a0and it is stored in\u00a0<code>.ssh<\/code>\u00a0directory under user\u2019s home directory. The\u00a0<code>~\/.ssh<\/code>\u00a0directory is automatically created when the user runs the ssh command for the first time.+<\/p>\n<div class=\"my-6 bg-yellow-lightest border-l-4 border-yellow-light note p-6 text-yellow-darkest\">\n<div class=\"flex\">\n<div class=\"w-full text-sm font-sans\">\n<p>If you have never used the ssh\u00a0<code>command<\/code>\u00a0first you\u2019ll need to create the directory using:<\/p>\n<p><code>mkdir -p ~\/.ssh &amp;&amp; chmod 700 ~\/.ssh<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>By default the SSH configuration file may not exist so you may need to create it using the\u00a0<a href=\"https:\/\/linuxize.com\/post\/linux-touch-command\/\">touch command<\/a>:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">touch ~\/.ssh\/config &amp;&amp; chmod 600 ~\/.ssh\/config<\/span><\/code><\/pre>\n<\/div>\n<p>This file must be readable and writable only by the user, and not accessible by others:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">chmod 700 ~\/.ssh\/config<\/span><\/code><\/pre>\n<\/div>\n<h2 id=\"ssh-config-file-structure-and-patterns\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#ssh-config-file-structure-and-patterns\" data-scroll=\"\">SSH Config File Structure and Patterns<\/a><\/h2>\n<p>The SSH Config File takes the following structure:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">Host hostname1<\/span>\r\n    <span class=\"na\">SSH_OPTION value<\/span>\r\n    <span class=\"na\">SSH_OPTION value<\/span>\r\n\r\n<span class=\"na\">Host hostname2<\/span>\r\n    <span class=\"na\">SSH_OPTION value<\/span>\r\n\r\n<span class=\"na\">Host *<\/span>\r\n    <span class=\"na\">SSH_OPTION value<\/span><\/code><\/pre>\n<\/div>\n<p>The contents of the SSH client config file is organized into stanzas (sections). Each stanza starts with the\u00a0<code>Host<\/code>\u00a0directive and contain specific SSH options that are used when establish connection with the remote SSH server.<\/p>\n<p>Indentation is not required, but is recommended since it will make the file easier to read.<\/p>\n<p>The\u00a0<code>Host<\/code>\u00a0directive can contain one pattern or a whitespace-separated list of patterns. Each pattern can contain zero or more non-whitespace character or one of the following pattern specifiers:<\/p>\n<ul>\n<li><code>*<\/code>\u00a0&#8211; matches zero or more characters. For example,\u00a0<code>Host *<\/code>\u00a0will match all host, while\u00a0<code>192.168.0.*<\/code>\u00a0will match all hosts in the\u00a0<code>192.168.0.0\/24<\/code>\u00a0subnet.<\/li>\n<li><code>?<\/code>\u00a0&#8211; matches exactly one character. The pattern,\u00a0<code>Host 10.10.0.?<\/code>\u00a0will match all hosts in\u00a0<code>10.10.0.[0-9]<\/code>\u00a0range.<\/li>\n<li><code>!<\/code>\u00a0&#8211; at the start of a pattern will negate its match For example,\u00a0<code>Host 10.10.0.* !10.10.0.5<\/code>will match any host in the\u00a0<code>10.10.0.0\/24<\/code>\u00a0subnet except\u00a0<code>10.10.0.5<\/code>.<\/li>\n<\/ul>\n<p>The SSH client reads the configuration file stanza by stanza and if more than one patterns match, the options from the first matching stanza takes precedence. Therefore more host-specific declarations should be given at the beginning of the file, and more general overrides at the end of the file.<\/p>\n<p>You can find a full list of available ssh options by typing\u00a0<code>man ssh_config<\/code>\u00a0in your terminal or by visiting the\u00a0<a href=\"https:\/\/man.openbsd.org\/OpenBSD-current\/man5\/ssh_config.5\">ssh_config man page<\/a>.<\/p>\n<p>The SSH config file is also read by other programs such as\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-scp-command-to-securely-transfer-files\/\"><code>scp<\/code><\/a>,\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-linux-sftp-command-to-transfer-files\/\"><code>sftp<\/code><\/a>\u00a0and\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization\/\"><code>rsync<\/code><\/a>.<\/p>\n<h2 id=\"basic-ssh-config-file-example\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#basic-ssh-config-file-example\" data-scroll=\"\">Basic SSH Config File Example<\/a><\/h2>\n<p>Now that we\u2019ve covered the basic of the SSH configuration file let\u2019s look at the following example.<\/p>\n<p>Usually, when you connect to a remote server via SSH you would specify the remote user name, hostname and post. For example, to connect as a user named\u00a0<code>john<\/code>\u00a0to a host called\u00a0<code>dev.example.com<\/code>\u00a0on port\u00a0<code>2322<\/code>\u00a0from the command line, you would type:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">ssh john@dev.example.com -p 2322<\/span><\/code><\/pre>\n<\/div>\n<p>If you like to connect to the server using the same options as provided in the command above simply by typing named\u00a0<code>ssh dev<\/code>\u00a0you\u2019ll need to put the following lines to your\u00a0<code>\"~\/.ssh\/config<\/code>\u00a0file:<\/p>\n<div class=\"code-label\">~\/.ssh\/config<\/div>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">Host dev<\/span>\r\n    <span class=\"na\">HostName dev.example.com<\/span>\r\n    <span class=\"na\">User john<\/span>\r\n    <span class=\"na\">Port 2322<\/span><\/code><\/pre>\n<\/div>\n<p>Now if you type:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">ssh dev<\/span><\/code><\/pre>\n<\/div>\n<p>the ssh client will read the configuration file and it will use the connection details that are specified for the\u00a0<code>dev<\/code>\u00a0host,<\/p>\n<h2 id=\"shared-ssh-config-file-example\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#shared-ssh-config-file-example\" data-scroll=\"\">Shared SSH Config File Example<\/a><\/h2>\n<p>This example gives more detailed information about the host patterns and option precedence.<\/p>\n<p>Let\u2019s take the following example file:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">Host targaryen<\/span>\r\n    <span class=\"na\">HostName 192.168.1.10<\/span>\r\n    <span class=\"na\">User daenerys<\/span>\r\n    <span class=\"na\">Port 7654<\/span>\r\n    <span class=\"na\">IdentityFile ~\/.ssh\/targaryen.key<\/span>\r\n\r\n<span class=\"na\">Host tyrell<\/span>\r\n    <span class=\"na\">HostName 192.168.10.20<\/span>\r\n\r\n<span class=\"na\">Host martell<\/span>\r\n    <span class=\"na\">HostName 192.168.10.50<\/span>\r\n\r\n<span class=\"na\">Host *ell<\/span>\r\n    <span class=\"na\">user oberyn<\/span>\r\n\r\n<span class=\"na\">Host * !martell<\/span>\r\n    <span class=\"na\">LogLevel INFO<\/span>\r\n\r\n<span class=\"na\">Host *<\/span>\r\n    <span class=\"na\">User root<\/span>\r\n    <span class=\"na\">Compression yes<\/span><\/code><\/pre>\n<\/div>\n<ul>\n<li>If you type\u00a0<code>ssh targaryen<\/code>\u00a0the ssh client will read the file and will apply the options from the first match which is\u00a0<code>Host targaryen<\/code>. Then it will check the next stanzas one by one for matching pattern. The next matching one is\u00a0<code>Host * !martell<\/code>\u00a0which means all hosts except\u00a0<code>martell<\/code>\u00a0and it will apply the connection option from this stanza. Finally the last definition\u00a0<code>Host *<\/code>\u00a0also mathes but the ssh client will take only the\u00a0<code>Compression<\/code>\u00a0option because the\u00a0<code>User<\/code>\u00a0option is already defined in the\u00a0<code>Host targaryen<\/code>\u00a0stanza. The full list of options used in this case is as follows:\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">HostName 192.168.1.10<\/span>\r\n<span class=\"na\">User daenerys<\/span>\r\n<span class=\"na\">Port 7654<\/span>\r\n<span class=\"na\">IdentityFile ~\/.ssh\/targaryen.key<\/span>\r\n<span class=\"na\">LogLevel INFO<\/span>\r\n<span class=\"na\">Compression yes<\/span><\/code><\/pre>\n<\/div>\n<\/li>\n<li>When running\u00a0<code>ssh tyrell<\/code>\u00a0the matching host patterns are:\u00a0<code>Host tyrell<\/code>,\u00a0<code>Host *ell<\/code>,\u00a0<code>Host * !martell<\/code>\u00a0and\u00a0<code>Host *<\/code>. The options used in this case are:\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">HostName 192.168.10.20<\/span>\r\n<span class=\"na\">User oberyn<\/span>\r\n<span class=\"na\">LogLevel INFO<\/span>\r\n<span class=\"na\">Compression yes<\/span><\/code><\/pre>\n<\/div>\n<\/li>\n<li>If you run\u00a0<code>ssh martell<\/code>\u00a0the matching host patterns are:\u00a0<code>Host martell<\/code>,\u00a0<code>Host *ell<\/code>\u00a0and\u00a0<code>Host *<\/code>. The options used in this case are:\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">HostName 192.168.10.50<\/span>\r\n<span class=\"na\">User oberyn<\/span>\r\n<span class=\"na\">Compression yes<\/span><\/code><\/pre>\n<\/div>\n<\/li>\n<li>For all other connections options specified in the\u00a0<code>Host * !martell<\/code>\u00a0and\u00a0<code>Host *<\/code>\u00a0sections will be used.<\/li>\n<\/ul>\n<h2 id=\"override-ssh-config-file-option\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#override-ssh-config-file-option\" data-scroll=\"\">Override SSH Config File Option<\/a><\/h2>\n<p>The ssh client receives its configuration in the following precedence order:<\/p>\n<ol>\n<li>Options specified from the command line<\/li>\n<li>Options defined in the\u00a0<code>~\/.ssh\/config<\/code><\/li>\n<li>Options defined in the\u00a0<code>\/etc\/ssh\/ssh_config<\/code><\/li>\n<\/ol>\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<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-ini\" data-lang=\"ini\"><span class=\"na\">Host dev<\/span>\r\n    <span class=\"na\">HostName dev.example.com<\/span>\r\n    <span class=\"na\">User john<\/span>\r\n    <span class=\"na\">Port 2322<\/span><\/code><\/pre>\n<\/div>\n<p>and you want to use all other options but to connect as user\u00a0<code>root<\/code>\u00a0instead of\u00a0<code>john<\/code>\u00a0simply specify the user on the command line:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">ssh -o \"User=root\" dev<\/span><\/code><\/pre>\n<\/div>\n<p>The\u00a0<code>-F<\/code>\u00a0(<code>configfile<\/code>) switch allows you to specify an alternative per-user configuration file.<\/p>\n<p>If you want your ssh client to ignore all of the options specified in your ssh configuration file, you can use:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">ssh -F \/dev\/null user@example.com<\/span><\/code><\/pre>\n<\/div>\n<h2 id=\"conclusion\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/using-the-ssh-config-file\/#conclusion\" data-scroll=\"\">Conclusion<\/a><\/h2>\n<p>You have learned how to configure your user ssh config file. You may also want to setup a\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-setup-passwordless-ssh-login\/\">SSH key-based authentication<\/a>\u00a0and connect to your Linux servers without entering a password.<\/p>\n<p><a href=\"http:\/\/www.linuxtoday.com\/security\/using-the-ssh-config-file-190106163517.html\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are regularly connecting to multiple remote systems over SSH on a daily basis, 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. One option would be to\u00a0create a bash alias\u00a0for each remote server connection. However, there is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/09\/linux-today-using-the-ssh-config-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Linux Today &#8211; Using the SSH Config File&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7726","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=7726"}],"version-history":[{"count":3,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7726\/revisions"}],"predecessor-version":[{"id":7998,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7726\/revisions\/7998"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=7726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=7726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=7726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}