Skip to main content
  1. Argv.Blog/

SSH Config File - Quick Connection

382 words·2 mins·
Linux Shell Sysadmin
Linux - This article is part of a series.
Part : This Article

Using aliases with SSH for quick & easy connections

To connect to a remote server seamlessly and effortlessly using any Terminal (Linux, MacOS), you can set up SSH configurations and aliases. This way, you can simply open a new terminal window and connect to your remote server with minimal effort. Here are the steps to achieve this:

Step 1: SSH Key Setup
#

  1. Generate SSH Key (if you don’t already have one):

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    Follow the prompts to save the key in the default location (~/.ssh/id_rsa) and optionally set a passphrase.

  2. Copy the SSH Key to Your Remote Server: Use the ssh-copy-id command to copy your SSH key to the remote server:

    ssh-copy-id username@remote_host
    

    Replace username and remote_host with your remote server’s username and hostname or IP address.

Step 2: SSH Config File
#

  1. Edit SSH Config File: Open or create the SSH config file (~/.ssh/config):

    nano ~/.ssh/config
    
  2. Add an Entry for Your Remote Server: Add an entry to simplify connecting to your remote server. Here’s an example configuration:

    Host myserver
        HostName remote_host
        User username
        IdentityFile ~/.ssh/id_rsa
    

    Replace myserver with a name you want to use for this connection, remote_host with your server’s hostname or IP address, and username with your remote server’s username.

Step 3: Automate Connection in the Terminal
#

  1. Create a Script or Alias: You can create a shell script or an alias to automate the connection. For example, you can add an alias to your shell configuration file (~/.zshrc, ~/.bashrc, or ~/.bash_profile depending on your shell).

    Open your shell configuration file:

    nano ~/.zshrc
    

    Add the following alias:

    alias connect_myserver='ssh myserver'
    

    Save and close the file, then reload the shell configuration:

    source ~/.zshrc
    

Step 4: Connect Using our Terminal
#

Now, you can open a new Terminal (Or reset the RC file) and simply type connect_myserver to connect to your remote server seamlessly.

Additional Tips:
#

  • Auto-Login: If you don’t want to enter a passphrase every time, you can use an SSH agent to manage your keys. Add the following to your shell configuration file:

    • Be cautious with this one
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    
  • Environment Variables: If you need to set environment variables or perform other setup tasks upon connecting, you can include them in your SSH config file using the RemoteCommand directive.

J Armando G
Author
J Armando G
Cybersecurity & General Tech Enthusiast
Linux - This article is part of a series.
Part : This Article