Insights

How to Specify an SSH Port When Connecting

Use ssh -p to connect on a specific port. Learn how to save the port in SSH config, select ports in scp and sftp, and check failed connections.

Specify an SSH Port When Connecting: Rotary selector switch with offset pointer, with the doxxnet wordmark.
On this page

Use a lowercase -p followed by the port number: ssh -p 2222 user@server.example.com. Swap in the real port, username and hostname for the server you're connecting to.

ssh -p 2222 user@server.example.com

SSH uses port 22 by default. When you specify a different port, you're changing the port your client connects to. You're not changing the port the server listens on.

In short:

  • ssh: lowercase -p. scp and sftp: uppercase -P.
  • Use a saved host entry in ~/.ssh/config to avoid typing the port every time.
  • A different port isn't a security boundary. Restrict who can reach the server.

Use the server's connection details

The right port is the one the server actually accepts for your authorized connection. Picking a different port at random won't move the SSH service there.

1. Get the connection details. Ask the administrator for the hostname or IP address, your authorized username and the configured SSH port. If the server accepts SSH on something other than the default, use the port they give you.

2. Open a terminal with OpenSSH. The command works with the OpenSSH client on Linux, macOS and Windows. Only connect to systems you're authorized to access.

3. Run the command with your own details.

ssh -p 2222 user@server.example.com

Here, 2222 is the destination port, user is the remote account and server.example.com is the destination hostname. Keep the option lowercase, and put it before the destination.

4. Check the outcome. A successful login means SSH was reachable and your authentication worked with those details. If the connection fails, use the troubleshooting steps below instead of changing the server blindly.

This command doesn't change any server configuration, so there's nothing to undo. To try corrected details, just run it again with the right values.

Save the port for repeat connections

A saved host entry lets you reuse the hostname, port and username through a short alias. And because an explicit port on the command line overrides the configured one, you can still pick a different port for a single connection.

1. Find your client configuration and back it up. On Linux and macOS, it's ~/.ssh/config. For Windows OpenSSH, it's the .ssh/config file under your Windows user profile. If the file already exists, make a backup before you edit it, and keep your other settings as they are.

2. Add or update a host-specific entry. Put it before any general defaults like Host *, not automatically at the end of the file.

Host myserver
    HostName server.example.com
    Port 2222
    User user

myserver is the alias you'll type, and HostName is the real destination. Replace the example values with the details you're authorized to use.

3. Check the effective configuration before you connect.

ssh -G myserver

Look at the HostName, Port and User values in the output. This command evaluates your configuration without opening a remote connection.

4. Connect through the alias.

ssh myserver

If the effective values match your intended destination, this uses your saved details. A successful login confirms the destination is reachable and your authentication works. Reading the configuration alone doesn't.

OpenSSH normally uses the first value it finds for each setting. A later duplicate entry doesn't override an earlier match. So if ssh -G myserver shows something unexpected, look at earlier matching entries and fix the relevant setting.

To undo the edit, restore your backup if you haven't made other changes since. Otherwise, remove only the new entry, or undo your specific changes, and leave the rest of the configuration alone.

Choose the correct option in other clients

Port syntax differs between clients. OpenSSH's ssh uses lowercase -p, while scp and sftp use uppercase -P. These terminal commands work wherever the matching OpenSSH tools are installed. PuTTY on Windows uses a field in its interface instead.

ClientPort optionExample or setting
OpenSSH sshLowercase -pssh -p 2222 user@server.example.com
OpenSSH scpUppercase -Pscp -P 2222 file.zip user@server.example.com:/home/user/
OpenSSH sftpUppercase -Psftp -P 2222 user@server.example.com
PuTTY on WindowsThe Port field on the Session screenEnter the hostname, set Port to 2222 and keep SSH as the connection type

The SCP example uploads the local file.zip to /home/user/ on the remote system. It needs authorized write access to that destination and may overwrite a file with the same name. Use a disposable example file and a destination you control, and back up any existing remote file before replacing it.

To undo a test upload, delete only the test file you created in that controlled destination. Deleting it won't bring back an original you overwrote. For that, you need your backup.

The SFTP command opens a file-transfer session on the port you chose. It doesn't upload anything by itself. With both transfer tools, use the SSH details you were given for your access, and don't assume the example port applies to your server.

Check a failed connection without guessing

Setting the port on the client is enough when the SSH service and the authorized network path already support that port. When they don't, you need to look beyond the client command. And the error message alone won't tell you the cause.

  1. Check the effective client destination. Review the hostname, username and -p value in your command. If you use a saved alias, run ssh -G myserver to see the effective destination, port and user without connecting. Remember that an explicit -p on the command line beats the saved port.
  2. Confirm the actual listening port. Ask the administrator which SSH port is available for your connection and whether the hostname or address is right. A port that used to work may no longer match the current service or access arrangement. Using -p doesn't configure sshd, open a firewall or set up port forwarding.
  3. Separate connection errors from authentication errors. "Connection refused" means the attempt was rejected. It doesn't prove nothing is listening. A timeout means the connection didn't finish in time, not necessarily that a firewall blocked it. An authentication rejection is a later stage, and it points to your username, credentials or account access.
  4. Investigate the authorized network path. For timeouts, check the network and destination address, then ask the administrator to review the access rules. Find out whether the destination needs a private route or another approved path. Opening public access shouldn't be your default fix.
  5. Keep recovery access during an administrator-led change. If the administrator recently changed the listening port, keep your working session open until the new setup is confirmed. Keep recovery access available too, and check that a separate connection works before you close the working one.

Correct client values tell you what you're trying to reach. They don't tell you the service is available. If your values are right and the connection still fails, give the administrator the destination, the port you chose and the exact error you saw, so they can look into the service and the access path.

Keep remote administration on an authorized private path

A nondefault port isn't an access-control boundary, and it doesn't make SSH undiscoverable. Changing the port doesn't, by itself, secure SSH, so don't treat it as a substitute for authentication or restricted access.

When you can, use your chosen port over an authorized private route. Restrict administration access instead of opening it to the whole internet. What matters is who can reach the service and authenticate, not whether its port is different from the default.

Keep choosing a destination port separate from forwarding. -p picks the port used to reach the SSH server. -L, -R and -D ask for forwarding, and -D gives you dynamic application-level port forwarding. You don't need a forwarding option just because the server accepts SSH on another port.

Ask an AI assistant

Share this article

Private Everywhere

Stop giving the internet everything

Keep your browsing, messages, files, and agents private.