How to Change the SSH Port

How to Change the SSH Port

Changing the default SSH port is a security measure that can help protect your server from automated attacks. Here are the steps to change the SSH port:

Note:

Before making changes to the SSH configuration, ensure that you have access to the server through another method, such as a physical console or another remote access method, to avoid being locked out if there are any issues.

For Linux Servers (e.g., Ubuntu, CentOS):

  1. Connect to Your Server:
    • Use your preferred SSH client to connect to your server using the current/default SSH port (usually 22).
  2. Edit the SSH Configuration File:
    • Open the SSH configuration file in a text editor. The location of this file may vary based on your Linux distribution.
      • For Ubuntu/Debian: /etc/ssh/sshd_config
      • For CentOS/RHEL: /etc/ssh/sshd_config
  3. Locate the “Port” Directive:
    • Find the line that starts with “Port” followed by the current SSH port (default is 22).
  4. Change the Port Number:
    • Replace the current port number with your desired port (e.g., 2222).
      Port 2222
  5. Save and Close the File:
    • Save the changes and close the text editor.
  6. Restart the SSH Service:
    • Restart the SSH service to apply the changes.
      sudo service ssh restart # For Ubuntu/Debian
      sudo systemctl restart ssh # For CentOS/RHEL

For Windows Servers (OpenSSH Server):

  1. Connect to Your Server:
    • Use an SSH client like PuTTY to connect to your server using the current/default SSH port (usually 22).
  2. Navigate to the SSH Configuration Directory:
    • Open File Explorer and go to C:\ProgramData\ssh (this is the default location for the OpenSSH server configuration on Windows).
  3. Edit the SSHD Configuration File:
    • Open the sshd_config file in a text editor.
  4. Locate the “Port” Directive:
    • Find the line that starts with “Port” followed by the current SSH port (default is 22).
  5. Change the Port Number:
    • Replace the current port number with your desired port (e.g., 2222).
      Port 2222
  6. Save the File:
    • Save the changes and close the text editor.
  7. Restart the SSH Server:
    • Open a PowerShell window as an administrator and restart the SSH server.
      Restart-Service sshd

Firewall Considerations:

After changing the SSH port, make sure to update your firewall settings to allow traffic on the new port. If you’re using a cloud provider, update the security group or firewall rules accordingly.

Connecting with the New Port:

When connecting to the server, use the -p option to specify the port:

ssh -p 2222 username@your_server_ip

Replace 2222 with the new port number you’ve configured.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.