Connecting to SSH Servers Through an Authenticated SOCKS5 Proxy

· Updated:

To SSH through a SOCKS5 proxy that needs a login, add ProxyCommand ncat --proxy IP:PORT --proxy-type socks5 --proxy-auth USER:PASS %h %p to the host's entry in ~/.ssh/config. OpenSSH has no built-in SOCKS client, so it hands the connection to a helper, and the common nc -X 5 helper cannot send a username and password. Below are working configs for SOCKS5 and HTTP CONNECT, ways to keep credentials out of logs, and fixes for the usual failures.

Reach SSH hosts that only allow one source IP

Outbound proxy versus ssh -D

Two different setups get called "SSH and SOCKS", and search results mix them up. With ssh -D 1080 server, SSH itself becomes a SOCKS server on your machine and forwards other apps' traffic through the remote host. That is a tunnel you create.

This article covers the opposite direction: your SSH client has to leave through someone else's SOCKS5 or HTTP proxy to reach a server. Typical reasons are a network that blocks outbound port 22, or a server whose firewall only accepts SSH from one source address. In this setup the proxy sees an encrypted SSH stream and nothing else; your keys and session contents stay end to end between client and server.

SSH ProxyCommand with ncat for SOCKS5 login

ncat ships with the Nmap project and supports SOCKS5 username and password authentication. Install it with sudo apt install ncat on Debian and Ubuntu, sudo dnf install nmap-ncat on Fedora and RHEL, or brew install nmap on macOS. Windows builds come with the Nmap installer.

# ~/.ssh/config
Host build-server
    HostName 198.51.100.25
    User deploy
    ProxyCommand ncat --proxy 203.0.113.10:PORT --proxy-type socks5 --proxy-auth USER:PASS %h %p

SSH replaces %h and %p with the target host and port, and ncat asks the proxy to open that connection. With this block in place, ssh build-server, scp, sftp, rsync -e ssh and Git over SSH all use the proxy without extra flags.

For a quick one-off test, the same thing works on the command line:

ssh -o ProxyCommand='ncat --proxy 203.0.113.10:PORT --proxy-type socks5 --proxy-auth USER:PASS %h %p' [email protected]

If the target is a hostname, ncat by default passes the name to the proxy to resolve remotely (see its --proxy-dns option). That matters for hosts that only resolve inside the proxy's network, and it keeps the lookup off your local resolver.

OpenBSD netcat: nc -X 5 works only without SOCKS auth

The nc installed by default on Debian, Ubuntu and macOS is the OpenBSD variant. It does speak SOCKS5 through -X 5 -x host:port, and many tutorials stop there:

ProxyCommand nc -X 5 -x 203.0.113.10:PORT %h %p

The catch is in its manual: the -P username option is only supported for HTTP CONNECT proxies. There is no way to give OpenBSD netcat a SOCKS5 username and password, so against an authenticated proxy the handshake simply fails. Use it for open SOCKS servers such as an ssh -D tunnel on localhost, and switch to ncat for anything that needs a login.

SSH via an HTTP proxy with CONNECT

If your proxy port runs in HTTP mode, SSH can travel inside a CONNECT tunnel. The proxy opens a raw TCP connection to the server's SSH port and relays bytes, exactly as it would for an HTTPS site.

ncat in HTTP mode

Change only the proxy type. ncat sends the credentials as HTTP Basic authentication.

Host build-server
    HostName 198.51.100.25
    ProxyCommand ncat --proxy 203.0.113.10:PORT --proxy-type http --proxy-auth USER:PASS %h %p

OpenBSD netcat with -X connect

Here netcat can authenticate, but it takes only the user name and prompts for the password on the terminal. That is fine for interactive logins and unusable for scripts or cron jobs.

ProxyCommand nc -X connect -x 203.0.113.10:PORT -P USER %h %p

Port restrictions on CONNECT

Many HTTP proxies only allow CONNECT to port 443, because that is what browsers need. A refusal that arrives right after login, often a 403, points to that rule. Try the same proxy in SOCKS5 mode, which has no notion of web ports, or run the SSH server on 443 if you control it.

Keeping the proxy password out of logs

Putting USER:PASS in ~/.ssh/config has side effects worth knowing. ssh -v prints the full proxy command, password included, in its debug output, and anyone on the same machine can read process arguments with ps while the session is open.

ncat offers a cleaner route: it reads credentials from the NCAT_PROXY_AUTH environment variable when --proxy-auth is absent. A small wrapper script loads them from a file only you can read, so they appear neither in the SSH config, nor in ssh -v output, nor in the process argument list.

#!/bin/sh
# ~/.ssh/socks-proxy.sh  (chmod 700)
# ~/.ssh/proxy-auth (chmod 600) holds one line: USER:PASS
NCAT_PROXY_AUTH="$(cat "$HOME/.ssh/proxy-auth")"
export NCAT_PROXY_AUTH
exec ncat --proxy 203.0.113.10:PORT --proxy-type socks5 "$@"
Host build-server
    ProxyCommand ~/.ssh/socks-proxy.sh %h %p

Root and your own user can still read a process's environment, which is the normal boundary on a Unix system. Note that SSH itself never sends your server password or private key to the proxy; only the proxy login is at stake here.

A fixed source IP for servers that trust one address

The strongest reason to put SSH behind a proxy is a firewall rule such as sudo ufw allow from 203.0.113.10 to any port 22 proto tcp, or a cloud security group that admits a single /32. Home and mobile connections change address, and a team spread across locations cannot all be listed. A dedicated proxy gives everyone one stable source address to permit.

ProxyV4 private datacenter IPv4 proxies are suited to that: one tenant per IP for the whole term, the same address while the proxy is active, and the same IP again when you renew before expiry, so the firewall rule never needs editing. Each proxy speaks SOCKS5 and HTTP(S) with username and password, and you can enable both on one port to use either ncat mode. Locations include the United States, the United Kingdom, Japan, Singapore, South Korea, Hong Kong and Vietnam.

Two honest caveats. If you control both ends, a VPN or a bastion host may be simpler than a third-party proxy. And if your workflow depends on UDP, for example mosh, ask support through a ticket before ordering, because only TCP is promised. You can buy one IP, run ssh -v through it, and check live per-IP prices on the pricing page.

Proxies, jump hosts and file transfers together

A common layout is proxy, then bastion, then private servers. Put the ProxyCommand on the bastion's entry and use ProxyJump for the hosts behind it. Do not set both options on one host: OpenSSH uses whichever appears first and ignores the other.

Host bastion
    HostName 198.51.100.30
    User ops
    ProxyCommand ncat --proxy 203.0.113.10:PORT --proxy-type socks5 --proxy-auth USER:PASS %h %p

Host app-*.internal
    ProxyJump bastion

Because scp, sftp and rsync all read ~/.ssh/config, they inherit this chain automatically. For applications that are not SSH-aware at all, a system-wide approach like the one in the Ubuntu proxy setup guide is the better fit.

Diagnosing failed connections

Start by testing the proxy on its own, outside SSH, with the proxy checker or curl. Then run ssh -v and read the lines right after the proxy command starts.

kex_exchange_identification: Connection closed by remote host

The helper connected to something, but no SSH banner came back. Common causes are a proxy that refused the destination after login, the wrong target port, or the server's own firewall dropping the proxy's address. Check the server's auth log for a connection attempt from the proxy IP.

Connection timed out during banner exchange

The tunnel opened but traffic stalls. Look for a protocol mismatch, such as --proxy-type http against a SOCKS-only port, or a proxy that is unreachable from your network. Running the ncat command by hand with -v shows whether the proxy handshake itself succeeds.

Authentication failures at the proxy

A rejected login comes from ncat, before SSH prints anything about keys. Recheck the user name and password, and copy the proxy line again if you recently switched between HTTP and SOCKS5 modes, since that can change the port.

Frequently asked questions

Can OpenSSH use a SOCKS5 proxy without extra tools?

No. OpenSSH has no SOCKS client of its own; it relies on a ProxyCommand helper such as ncat or netcat to make the connection through the proxy.

Why does nc -X 5 fail with my SOCKS5 username and password?

OpenBSD netcat supports proxy authentication only for HTTP CONNECT, never for SOCKS5. Use ncat --proxy-type socks5 --proxy-auth USER:PASS instead.

Is SSH through a proxy still encrypted?

Yes. The proxy relays the SSH stream but cannot decrypt it, and your private key and server password never reach the proxy.

Can I use the same proxy for scp and rsync?

Yes. Both use the SSH client, so a ProxyCommand in ~/.ssh/config applies to them automatically.

What happens to my firewall rule when the proxy renews?

With a ProxyV4 private IPv4, renewing before expiry keeps the same address, so the rule stays valid. If a proxy fails for a technical reason, the refund policy credits your account balance.

Reach SSH hosts that only allow one source IP

Get a private IPv4 with SOCKS5 and HTTP(S) login on one port and an address that stays the same while active. Start with a single IP and test your SSH setup.

Get a static IP

Related articles

Knowledge base
HTTP or SOCKS5: Picking the Proxy Protocol Your Software Needs
HTTP vs SOCKS5 proxy in plain terms: what each protocol carries, why HTTPS works through both, where UDP matters, and how to test which one a server speaks.
Proxy setup
Configuring a Proxy on Ubuntu and Debian, Layer by Layer
Set an authenticated APT proxy on Ubuntu or Debian, then the shell, systemd services, sudo and GNOME. Copy-paste config files and checks that prove it works.
Free tools
Check proxies in bulk: status, exit IP and latency
Free online proxy checker: paste up to 100 ip:port:user:pass lines and see which proxies work, their exit IP and latency, plus what each failure result means.
Products
Dedicated datacenter proxies: one private IPv4 per customer
Private IPv4 proxies in the US, Vietnam, Japan, South Korea, Singapore, the UK and Hong Kong. One tenant per IP, HTTP(S) and SOCKS5, no traffic metering.
Proxy setup
Set up your proxy, tool by tool
Proxy setup instructions you can follow with the dashboard open: antidetect browsers, operating systems, browser extensions and scraping libraries, all with authenticated HTTP(S) or SOCKS5.
Knowledge base
Setting Up proxychains-ng With an Authenticated SOCKS5 Proxy
Proxychains SOCKS5 with username and password: the ProxyList line, which proxychains4.conf is read, proxy_dns, chain types and fixing Go tools that bypass it.
Knowledge base
Routing Invoke-WebRequest and Invoke-RestMethod Through an Authenticated Proxy
Use a PowerShell Invoke-WebRequest proxy with -ProxyCredential, set a session default proxy, and handle the gaps between Windows PowerShell 5.1 and 7.
Knowledge base
Authenticated Proxies in undetected-chromedriver: Extension or Local Forwarder
Set an undetected chromedriver proxy with a login: why --proxy-server drops credentials, an MV3 auth extension, a local forwarder, and how to check the exit IP.
Products
Static ISP proxies: residential IPs on consumer networks
Static ISP proxies in the United States and Vietnam: fixed IPs registered to consumer internet providers, rented to one customer, with unmetered traffic.
Pricing
Proxy prices per IP, by location and volume
Current per-IP rates for private IPv4 and static ISP proxies in the US, Vietnam, Japan, South Korea, Singapore, the UK and Hong Kong, with volume discounts.