DEV Community

Sam Newby
Sam Newby

Posted on

Simplify Your Server Connections with SSH Config

If you're managing multiple servers, typing out full SSH commands with usernames, IP addresses, and specific keys can become tedious. Let's explore how to use SSH config to make your server connections simpler and more efficient.

Understanding SSH Config

The SSH config file lets you create shortcuts for your SSH connections. Instead of typing:

ssh -i ~/.ssh/staging_key username@203.0.113.1 -p 2222
Enter fullscreen mode Exit fullscreen mode

You can simply type:

ssh staging
Enter fullscreen mode Exit fullscreen mode

Setting Up Your SSH Config

  1. Create or open your SSH config file:
nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  1. Set the file permissions (if it's a new file):
chmod 600 ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Basic Configuration Examples

Here's a simple configuration for a single server:

Host webserver
    HostName 203.0.113.1
    User admin
    Port 22
    IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Now you can connect by simply typing:

ssh webserver
Enter fullscreen mode Exit fullscreen mode

Advanced Configuration Examples

Multiple Servers with Different Settings

# Production Server
Host prod
    HostName 203.0.113.1
    User produser
    Port 22
    IdentityFile ~/.ssh/prod_key

# Staging Server
Host staging
    HostName 203.0.113.2
    User stageuser
    Port 2222
    IdentityFile ~/.ssh/staging_key

# Development Server
Host dev
    HostName 203.0.113.3
    User devuser
    IdentityFile ~/.ssh/dev_key
Enter fullscreen mode Exit fullscreen mode

Using Wildcards

Connect to multiple servers with similar patterns:

# All development servers
Host dev-*
    User developer
    IdentityFile ~/.ssh/dev_key
    Port 22

# Matches dev-01, dev-02, etc.
Host dev-01
    HostName 203.0.113.11

Host dev-02
    HostName 203.0.113.12
Enter fullscreen mode Exit fullscreen mode

Useful SSH Config Options

Connection Settings

Host myserver
    HostName 203.0.113.1
    User admin
    Port 22
    IdentityFile ~/.ssh/custom_key
    AddKeysToAgent yes
    ForwardAgent yes
    Compression yes
Enter fullscreen mode Exit fullscreen mode

Keeping Connections Alive

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 5
    TCPKeepAlive yes
Enter fullscreen mode Exit fullscreen mode

Jump Hosts (Bastion Servers)

Host private-server
    HostName 10.0.0.5
    User admin
    ProxyJump bastion
    IdentityFile ~/.ssh/private_key

Host bastion
    HostName 203.0.113.1
    User jumpuser
    IdentityFile ~/.ssh/bastion_key
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Organization

    • Group related hosts together
    • Use comments to document configurations
    • Keep sensitive production configs separate
  2. Security

    • Use specific IdentityFile for each server
    • Avoid using passwords when possible
    • Set proper file permissions (600)
  3. Default Settings

Host *
    UseKeychain yes
    AddKeysToAgent yes
    IdentitiesOnly yes
    HashKnownHosts yes
Enter fullscreen mode Exit fullscreen mode

Practical Use Cases

Development Environment

# Local Development VMs
Host dev-vm
    HostName localhost
    User developer
    Port 2222
    StrictHostKeyChecking no

# GitHub
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_key
    AddKeysToAgent yes
Enter fullscreen mode Exit fullscreen mode

Cloud Servers

# AWS Servers
Host aws-*
    User ec2-user
    IdentityFile ~/.ssh/aws_key

# Digital Ocean Droplets
Host do-*
    User root
    IdentityFile ~/.ssh/do_key
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Tips

  1. Connection Issues

    • Use -v flag for verbose output:
     ssh -v myserver
    
  • Check file permissions
  • Verify IdentityFile paths
  1. Config File Not Working
    • Ensure correct file permissions (600)
    • Check syntax and indentation
    • Verify file location (~/.ssh/config)

Advanced Features

Multiplexing Connections

Host *
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h-%p
    ControlPersist 600
Enter fullscreen mode Exit fullscreen mode

Different Keys for Different Ports

Match host * port 2222
    IdentityFile ~/.ssh/special_key
Enter fullscreen mode Exit fullscreen mode

Conclusion

A well-organized SSH config file can significantly streamline your server management workflow. Start with basic configurations and gradually add more advanced features as needed.

Remember to keep your SSH keys secure and regularly update your configurations to reflect your current server infrastructure.

Need help optimizing your server connections? Feel free to reach out to our support team.

Top comments (0)