ssh — remote server connection
💬 "ssh command"
ssh opens an encrypted shell session to a remote server. It is the fundamental access path for server operations.
In practice, you use key authentication instead of passwords, together with options such as -p/-i/-L.
- Concept: An encrypted protocol that connects to a remote host and executes commands.
- Left: The connection target in
user@hostformat. - Options: Specify -p port, -i key file, -L tunnel, etc.
- Exit: Close the session with
exitor Ctrl+D.
Commonly used options
| Option | Description |
|---|---|
| -p <port> | Connect to a port other than the default 22 |
| -i <key file> | Specify the private key to use |
| -L <local>:<target>:<port> | Creates local port forwarding (tunnel) |
| -v | Prints the connection process in detail to diagnose problems |
3 essential command combinations
ssh -p 22022 deploy@10.0.0.9— Connect to the deployment account on a non-standard portssh -i ~/.ssh/id_ed25519_gamehub root@10.0.0.9— Connect as admin using the specified keyssh -L 5433:127.0.0.1:5432 deploy@10.0.0.9— Tunnel a remote DB to a local port
💬 "Show me an example of using this to improve deployment stability"
Here are common situations on real production servers, organized in order.
Step 1 · Check reachability
First, check whether the port is open.
nc -vz 10.0.0.9 22 || nc -vz 10.0.0.9 22022
Step 2 · Connect
Connect by specifying the key and port.
ssh -i ~/.ssh/deploy_ed25519 -p 22022 deploy@10.0.0.9
Step 3 · Diagnose if there is a problem
If the connection doesn't work, use -v to see which step is failing.
ssh -v -p 22022 deploy@10.0.0.9
This is how it is used in practice
1. Emergency access to a production server
Situation — You receive an incident alert and need to get into a production server.
ssh -i ~/.ssh/prod_ed25519 ops@10.0.0.9
Result — You're connected immediately without a password using key authentication.
Point — Set up key authentication in advance so you don't have to look for a password in an emergency.
2. Remote DB tunnel
Situation — You cannot connect directly to the DB from your local machine because of a firewall.
ssh -N -L 5433:127.0.0.1:5432 ops@10.0.0.9
Result — Local port 5433 is connected to remote port 5432, so you can access it with local tools.
Point — -N opens only the tunnel without a shell. Close the session after you're done to clean up the tunnel.
Things to watch out for
- Direct root access is not recommended. Use a deployment account + sudo.
- Key file permissions must be 600. If they are too open, ssh will refuse.
- Tunnels (-L) are valid only while the connection is maintained. If the connection drops, you must reopen the tunnel.
- A host key change warning can indicate a man-in-the-middle attack, so do not blindly delete known_hosts.
Actual execution screen
This is the result of accessing the production server with key authentication and checking its status.
!ssh command execution result: remote access and status check
Production server in operation
Source (HTML): https://games.aidebatehub.com/programs/linux/remote/ssh.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments