|
| 1 | +--- |
| 2 | +title: "SSH Connection Manager in Command Line" |
| 3 | +excerpt: "Managing multiple SSH connections and hosts can sometimes be problematic. In this post, I will show how you can easily save multiple connections directly in the terminal (Linux) to take advantage of tab completion, passwordless authentication, and more." |
| 4 | +categories: |
| 5 | + - Linux |
| 6 | + - Howto |
| 7 | + |
| 8 | +tags: |
| 9 | + - Linux |
| 10 | + - Tips |
| 11 | + - HowTo |
| 12 | + |
| 13 | +toc: true |
| 14 | +header: |
| 15 | + teaser: "/assets/images/ssh_logo.jpg" |
| 16 | +--- |
| 17 | + |
| 18 | +## Managing SSH Connections |
| 19 | + |
| 20 | +In the Windows ecosystem, tools like Remote Desktop Manager help handle and manage multiple connections to the various servers within the infrastructure. But how can we achieve something similar for SSH connections to frequently accessed servers in Linux? |
| 21 | + |
| 22 | +Let’s explore how this can be done efficiently in the command line. |
| 23 | + |
| 24 | +## SSH Config file |
| 25 | + |
| 26 | +First, if it doesn’t already exist, create the .ssh directory in a convenient location (I keep mine in my $HOME directory): |
| 27 | + |
| 28 | +```bash |
| 29 | +mkdir .ssh |
| 30 | +``` |
| 31 | + |
| 32 | +Next, create a file named config, adjust its default permissions, and open it with your favorite text editor: |
| 33 | + |
| 34 | +```bash |
| 35 | +touch config |
| 36 | + |
| 37 | +chmod 600 config |
| 38 | + |
| 39 | +vim config |
| 40 | +``` |
| 41 | + |
| 42 | +This file is automatically sourced by the terminal, allowing you to easily connect to machines defined in it. Here's a sample of the configuration I use to manage my servers: |
| 43 | + |
| 44 | +```bash |
| 45 | +Host node_01 |
| 46 | + Hostname node01.lab.com |
| 47 | + User pscustomobject |
| 48 | + IdentityFile /home/pscustomobject/.ssh/id_rsa |
| 49 | +Host node_02 |
| 50 | + Hostname node02.example.com |
| 51 | + User adminuser |
| 52 | + Port 4242 |
| 53 | + |
| 54 | +## Set connection defaults for all hosts, this is overriden by host options |
| 55 | +Host * |
| 56 | + ForwardAgent no |
| 57 | + ForwardX11 no |
| 58 | + ForwardX11Trusted yes |
| 59 | + User pscustomobject |
| 60 | + Port 22 |
| 61 | + Protocol 2 |
| 62 | + ServerAliveInterval 60 |
| 63 | + ServerAliveCountMax 30 |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +## Key Notes |
| 68 | + |
| 69 | +- The IdentityFile directive points to the location of your private key. This can be stored in a shared or networked location for easier access across multiple devices. |
| 70 | +- The *Host ** section defines defaults for all hosts, which individual host entries can override. |
| 71 | + |
| 72 | +Once the SSH configuration is in place, you can connect to any of the defined hosts simply by issuing the command: |
| 73 | + |
| 74 | +```bash |
| 75 | +ssh node_01 |
| 76 | +``` |
| 77 | + |
| 78 | +Better yet, tab completion will work just as it does for standard commands, so you don’t have to remember the exact name of each node. |
| 79 | + |
| 80 | +## Additional Tips |
| 81 | + |
| 82 | +I highly recommend reading the *ssh_config* man page (*man ssh_config*) to discover the numerous other options you can use in your config file to further streamline and simplify SSH connection management. |
0 commit comments