Table of Contents
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):
- Connect to Your Server:
- Use your preferred SSH client to connect to your server using the current/default SSH port (usually 22).
- 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
- For Ubuntu/Debian:
- Open the SSH configuration file in a text editor. The location of this file may vary based on your Linux distribution.
- Locate the “Port” Directive:
- Find the line that starts with “Port” followed by the current SSH port (default is 22).
- Change the Port Number:
- Replace the current port number with your desired port (e.g., 2222).
Port 2222
- Replace the current port number with your desired port (e.g., 2222).
- Save and Close the File:
- Save the changes and close the text editor.
- 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
- Restart the SSH service to apply the changes.
For Windows Servers (OpenSSH Server):
- Connect to Your Server:
- Use an SSH client like PuTTY to connect to your server using the current/default SSH port (usually 22).
- 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).
- Open File Explorer and go to
- Edit the SSHD Configuration File:
- Open the
sshd_config
file in a text editor.
- Open the
- Locate the “Port” Directive:
- Find the line that starts with “Port” followed by the current SSH port (default is 22).
- Change the Port Number:
- Replace the current port number with your desired port (e.g., 2222).
Port 2222
- Replace the current port number with your desired port (e.g., 2222).
- Save the File:
- Save the changes and close the text editor.
- Restart the SSH Server:
- Open a PowerShell window as an administrator and restart the SSH server.
Restart-Service sshd
- Open a PowerShell window as an administrator and restart the SSH server.
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.