使用config快速登陆服务器

To streamline your connection process, you can create an SSH configuration file or a custom script. Here’s how to set up each:

set up a SSH configuration file to streamline your connection process

  1. Open or create an SSH configuration file:
    nano ~/.ssh/config

  2. Add an entry for your server:

Host myserver
    HostName remoteIP
    User root
    IdentityFile /path/to/your/privatekey.pem
  1. Save the file (Ctrl+O, then Enter, and Ctrl+X).

  2. Set the correct permissions for the config file:
    chmod 600 ~/.ssh/config

Now, you can connect to your server using: ssh myserver

If you have several servers to connect to

The best approach is to use the SSH configuration file (~/.ssh/config) to define all your server details in one place. Here’s how you can organize it in the configuration file:

# Server 1
Host server1
    HostName 192.168.1.1
    User root
    IdentityFile /path/to/server1-key.pem

# Server 2
Host server2
    HostName 203.0.113.1
    User admin
    IdentityFile /path/to/server2-key.pem

# Server 3
Host mydbserver
    HostName 198.51.100.1
    User dbadmin
    IdentityFile /path/to/dbserver-key.pem
    Port 2222

You can now connect to each server using its alias (the Host value) defined in the configuration file. For example:

ssh server1
ssh server2
ssh mydbserver

Use Wildcards for Common Settings

If multiple servers share the same settings (e.g., user or identity file), you can use wildcards to reduce redundancy:

In configuration file:

# Shared settings for all servers
Host *.example.com
    User commonuser
    IdentityFile /path/to/common-key.pem

# Specific server overrides
Host web.example.com
    HostName 192.168.1.100

Host db.example.com
    HostName 192.168.1.101
    Port 2222

Now you can connect to web.example.com or db.example.com using:

ssh web.example.com
ssh db.example.com

Benefits of This Approach
1. Centralized Management: All server details are stored in one file.
2. Shortened Commands: No need to repeatedly type long ssh commands.
3. Customizability: Easily add, update, or remove server configurations.