Ubuntu桌面环境下,如何SSH连接无法直接访问的服务器?
Alright, let's cut through that annoying VM/PuTTY hoopla and get your Ubuntu Desktop (D) connected directly to server S. Dealing with environment mismatches, broken copy-paste, and wonky Alt-Tab is no fun—here are three solid fixes to solve this for good:
Since you can already reach S via your Windows VM, we can use that VM as a middleman to forward SSH traffic from your Ubuntu host. Here's how:
- First, make sure your Windows VM and Ubuntu desktop are on the same network (use bridge mode or host-only networking in your hypervisor) and can ping each other.
- On your Ubuntu terminal, run this command to set up local port forwarding:
Breakdown:ssh -L 2222:S_IP:22 windows_vm_username@windows_vm_ip-Ltells SSH to forward traffic from your Ubuntu's port 2222 to server S's port 22, routed through the Windows VM. Replace the placeholders with your actual VM username/IP and server S's IP. - Enter your Windows VM's password when prompted, and keep this terminal window open. Now open a new Ubuntu terminal and connect directly to your local forwarded port:
This drops you straight into server S, using Ubuntu's native terminal—copy-paste and Alt-Tab will work like normal.ssh s_username@localhost -p 2222
If you want to skip typing long commands every time, set up your SSH config file to handle the jump automatically:
- Open your Ubuntu SSH config file in a text editor:
nano ~/.ssh/config - Paste in this configuration (replace placeholders with your actual details):
Host windows-vm HostName windows_vm_ip User windows_vm_username Host server-s HostName S_IP User s_username ProxyJump windows-vm - Save and close the file (Ctrl+O, Enter, then Ctrl+X in nano). Now you can connect to server S with a single command:
SSH will automatically connect to your Windows VM first, then jump to server S—all in your native Ubuntu terminal.ssh server-s
The most permanent fix is to bypass the firewall block entirely, if you manage the firewall or server S:
- On server S (if it uses UFW), allow incoming SSH from your Ubuntu desktop's IP:
ufw allow from D_IP to any port 22 - If using iptables instead:
iptables -A INPUT -s D_IP -p tcp --dport 22 -j ACCEPT
Once this rule is applied, you can connect directly from Ubuntu to server S with a simple ssh s_username@S_IP—no VM required.
Quick Notes:
- For method 1/2, make sure your Windows VM has OpenSSH Server enabled (you can install it via Windows Settings > Apps > Optional Features).
- If your Ubuntu has an older SSH version (pre-7.3), replace
ProxyJumpwithProxyCommand ssh -W %h:%p windows-vmin the config file.
内容的提问来源于stack exchange,提问作者blueFast

