/ html / wiki / linux / HowDoI / short-ssh-commands.html
short-ssh-commands.html
  1  <!DOCTYPE html>
  2  <html lang="de">
  3     <head>
  4        <meta charset="UTF-8" />
  5        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6        <meta http-equiv="onion-location" content="http://bopbopl6lohkl2rts3ltesjnag4hzs4jrx2h6k6etgq5xasbpqekzlqd.onion" />
  7        <title>BOP Wiki: Shorten SSH Commands with config</title>
  8        <link rel="stylesheet" href="/assets/stylesheet.css" />
  9        <link rel="icon" type="image/x-icon" href="/assets/img/favicon.png">
 10     </head>
 11     <body>
 12        <header>
 13           <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 14           <script src="/assets/js/navbar-OpenClose.js"></script>
 15           <script src="/assets/js/lightbox.js"></script>
 16            <script src="/assets/js/copyCodeButton.js"></script>
 17           <link rel="stylesheet" href="/resources/js-libraries/highlightJS/atom-one-dark.min.css">
 18           <script src="/resources/js-libraries/highlightJS/highlight.min.js"></script>
 19           <script src="/resources/js-libraries/highlightJS/highlightjs-line-numbers.min.js"></script>
 20           <script>hljs.highlightAll();</script>
 21           <script>hljs.initLineNumbersOnLoad();</script>
 22           <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 23           <div class="branding">
 24              <button class="toggle-btn-navbar" id="navbarOpenButton">☰</button>
 25              <a href="/">
 26              <img class="logo" src="/assets/img/logo.png">
 27              </a>
 28              <div class="typing-animation">BytesOfProgress</div>
 29           </div>
 30        </header>
 31        <div id="navbarContainer" class="navbar-container">
 32           <iframe class="navbar-iframe" src="/assets/navbar/navbar.html" frameBorder= "0"></iframe>
 33        </div>
 34        <main>
 35           <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 36           <article class="site-post">
 37              <header class="post-header">
 38                 <h1 class="post-title">Shorten SSH Commands with config</h1>
 39                 <div class="post-meta">
 40                 </div>
 41              </header>
 42           </article>
 43           <nav class="breadcrumb">
 44              <a href="/">Home</a>
 45              <span class="divider">›</span>
 46              <a href="/wiki/">Wiki</a>
 47              <span class="divider">›</span>
 48              <a href="/wiki/linux/A1linux.html">GNU / Linux</a>
 49              <span class="divider">›</span>
 50              <a href="/wiki/linux/HowDoI/A1HowDoI.html">HowDoI</a>
 51              <span class="divider">›</span>
 52              <span class="current">Shorten SSH Commands with config</span>
 53           </nav>
 54           <section class="post-content">
 55              <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 56              <p>
 57                 This will be all about less typing.
 58                 When we normally connect to a machine via SSH, we type something like this:
 59              </p>
 60  <blockquote>
 61    $ ssh user@ssh.domain.com
 62  </blockquote>
 63              <p>
 64                 or:
 65              </p>
 66  <blockquote>
 67    $ ssh user@ip-address
 68  </blockquote>
 69              <p>
 70                 But when we connect to a server quite often, this will get annoying quickly.
 71                 We can use a config file to make establishing a connection much easier and faster.
 72              </p>
 73              <p>
 74                 In most distributions, this config file has 2 locations:
 75                 "/etc/ssh/ssh_config" for system-wide config, and "~/.ssh/config" for only
 76                 the user. Some distributions might only have the system-wide config.
 77                 In most cases, if the user's file is non-existent, you can just create it
 78                 and it will work just fine.
 79                 Yes, we could also create aliases for some of these configs in our ~/.bashrc,
 80                 but it is considered good practice to use the ssh_config.
 81              </p>
 82  
 83              <span style="color:#ff6600">
 84                 <h1 style="font-size:25px">Hostname alias config</h1>
 85              </span>
 86  
 87              <div class="code-box">
 88                 <pre><code>Host test
 89      HostName ssh.domain.com</code></pre>
 90              </div>
 91  
 92              <p>
 93                 This will shorten the command from the beginning to this:
 94              </p>
 95  <blockquote>
 96    $ ssh user@test
 97  </blockquote>
 98              <p>
 99                 You can also create multiple hostname aliases for the same host, like this:
100              </p>
101              <div class="code-box">
102                 <pre><code>Host test sample dummy
103      HostName ssh.domain.com</code></pre>
104              </div>
105  
106              <p>
107                 With this config we can either use test, sample or dummy in our SSH command to
108                 connect to the same machine.
109              </p>
110              <p>
111                 If the server has multiple subdomains you would like to be able to use in
112                 your command, you can use hostname aliases like this:
113              </p>
114  
115              <div class="code-box">
116                 <pre><code>Host test sample dummy
117      HostName %h.domain.com</code></pre>
118              </div>
119  
120              <p>
121                 With this, you can use test, sample or dummy in your SSH command, and SSH will
122                 hang put your alias as the subdomain for the host. For example:
123              </p>
124  <blockquote>
125    $ ssh user@test # This will connect you to test.domain.com
126  </blockquote>
127  
128  <blockquote>
129    $ ssh user@sample # This will connect you to sample.domain.com
130  </blockquote>
131  
132              <span style="color:#ff6600">
133                 <h1 style="font-size:25px">Pre-configuring the username</h1>
134              </span>
135  
136              <p>
137                 We can shorten the command even further by pre-configuring the user in
138                 the config file. This will come in handy if you always connect to the machine
139                 as the same user.
140              </p>
141              <div class="code-box">
142                 <pre><code>Host test
143      HostName ssh.domain.com
144      User admin</code></pre>
145              </div>
146  
147              <p>
148                 With this config you would connect to "ssh.domain.com" as the user "admin",
149                 just by typing this command:
150              </p>
151  
152  <blockquote>
153    $ ssh test
154  </blockquote>
155              <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
156           </section>
157           <footer class="post-footer">
158              <a href="/wiki/linux/HowDoI/A1HowDoI.html" class="cta-button">← Back</a>
159           </footer>
160        </main>
161     </body>
162  </html>