How to Use SSH to Connect to a Remote Server and Different Port

SSH (Secure Shell) is the standard way to log into remote Linux servers over an encrypted connection. From your Ubuntu workstation or any client, run ssh username@server_ip, accept the host key on first connect, then authenticate with a password or—preferably—an SSH key pair.

An SSH server that listens away from port 22 will not answer a normal ssh user@host connection. Supplying the alternate port sends the client to the actual sshd endpoint, which separates a reachable login service from a network failure before authentication starts.

The OpenSSH client accepts the port on the command line for one connection, or from a Port directive inside a matching Host block. A saved alias in ~/.ssh/config keeps the hostname, user, port, and optional key together so repeated commands do not depend on remembering -p each time.

The server must already listen on the alternate TCP port, and every firewall, security group, or NAT rule in the path must allow that port. A first connection to the same hostname on a new port can still show a host-⁠key prompt because OpenSSH tracks non-⁠default endpoints as [host]:port entries.

Steps to connect to an

SSH server

on a different port:

  1. Confirm that the server and network path are intended to use the alternate SSH port.

    The client-⁠side command does not move the server listener. Configure sshd and any firewall rule first, then use this page to connect to that published endpoint.

  2. Connect with the alternate port on the command line.
    $ ssh -p 2222 user@host.example.net whoami
    user

    Omit whoami to open an interactive shell instead of running a one-⁠command login test.

  3. Save the alternate port in a per-⁠user SSH client alias when the same endpoint is used regularly.
    Host host-alt-port
        HostName host.example.net
        User user
        Port 2222
        IdentityFile ~/.ssh/id_ed25519

    Omit IdentityFile when the default key or ssh-⁠agent already supplies the right identity. 

  4. Show the resolved client configuration for the alias.
    $ ssh -G host-alt-port
    host host-alt-port
    user user
    hostname host.example.net
    port 2222
    ##### snipped #####
    identityfile ~/.ssh/id_ed25519

    ssh -G prints the final client settings after matching Host blocks and defaults are applied, without opening a network session. 

  5. Connect through the saved host alias.
    $ ssh host-alt-port whoami
    user

    The same alias can be reused with scp and sftp so those clients inherit the configured hostname, user, port, and key.

 
  • Remote Server, SSH server, different port
  • 0 Kunder som kunne bruge dette svar
Hjalp dette svar dig?

Relaterede artikler

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to remove a Linux user

How do I drop or remove old users account from my Linux server? I can login using the user’s SSH...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Powered by WHMCompleteSolution