Atlantic.Net Blog

How to Change the SSH Port in CentOS

Verified and Tested 4/21/15

Introduction

In this how-to, we will be showing you how to change your SSH port in CentOS. We created a brand new CentOS 6.5 64 bit Cloud server for the exact operating system. We will be using the vi editor for our file editing needs. Feel free to download and use any editor that you feel comfortable with.

Believe it or not, one of the simplest things you can do to secure your server is to change the SSH port. Since SSH comes on a default port of 22, you will see a lot of brute force attacks occurring over that port because a lot of users do not change this default SSH port!

Prerequisites

A server installed with CentOS 6.5 x86_64.  If you currently do not have a server, please consider VPS Hosting from Atlantic.Net.

Changing your SSH port in CentOS

The first thing we want to do is SSH into our server using your preferred SSH client. Be sure that if you are using a custom user, that you have sudo rights. In this step, we will be performing commands as the root user.

Log into Root shell

Root Shell

Once you have logged into the server, let’s open our SSH configuration.

vi /etc/ssh/sshd_config

Once you are in the configuration, you’ll want to look for the section that has “Port.” It should be near the top and shown as “#Port 22.”

Edit "Port" directive in sshd_config

Sample sshd_config

We will be removing the ‘#’ so that it is a plain line of “Port 22”. Next, we will be changing the 22 in the line to be any number you’d like that another program does not run on. For a list of common ports in use, you can check the following MIT page. In our example, we will be changing the SSH port to 922, so your new line should read:

Port 922

Once you have the change done, exit and save the sshd_conf file. Now all you need to run is the below command, and it will restart the SSH server. The next time you want to connect via SSH, you will need to do so on your new port, in our case, 922.

service sshd restart

Don’t forget to change the port 22 to your new port in your IPTables when done! You can check out the Locking down your CentOS server with IPTables section (link) for information on how to do that.

Note: *If using Atlantic.net Cloud services, You can always access your server via our VNC viewer in the Cloud Portal if you lock yourself out*

Learn more about our VPS hosting services and VPS hosting price.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year