Change your SSH port

Change your SSH port

It is common to see the default ports are brute-forced attacks entries in your log files. Some of the common ones like part 25, 110 etc. Not limited to port 22, the default port for SSH.

If you have a VPS or a dedicated server, you will have SSH with root access. For convenience, they are delivered with SSH default port 22. However, we encourage the user to change their SSH port from the default 22 to another port, and it is not conflicted with other services.

In today’s example, we use port 1222. Since we have decided to use port 1222, we have to make sure you can connect to port 1222, so we must open up the port 1222 in order to SSH remotely.

For CentOS using firewalld we do this;

  • sudo firewall-cmd –permanent –remove-service=ssh
  • sudo firewall-cmd –permanent –add-port=1222/tcp

For CentOS using iptables do this;

  • iptables -A INPUT -p tcp -m tcp –dport 1222 -j ACCEPT
  • Remember to save

Now port 1222 is opened. We can proceed up the SSH port. Edit using vi /etc/ssh/sshd_confug

go to #port 22 press ‘i to update to port 1222 and save it with this command, press ESC mah=ke sure you are loner seeing the word  ‘insert’ at the left below screen use this command to save :wq

Now, we reboot the server and all the services will be restarted with the new value. Congratulation! You have changed your SSH port to 1222. Should you face any difficulty with this please contact our support team.


Google Authenticator


To protect your SSH server with an two-factor authentication, you can use the Google Authenticator PAM module. Each time you are connecting to your server via SSH with Google Authenticator PAM installed  you have to enter the code from your smartphone with Google Authenticator installed.

On Red Hat, CentOS and Fedora systems install the ‘pam-devel‘ package.

# yum install pam-devel make gcc-c++

Install Wget if you have not installed one yet

TOTP (timebased one-time-password) security tokens are time sensitive. Hence, make sure that your system has ntpd running, and is configured to start the service at boot

# service ntpd start
# chkconfig  ntpd on

Download and extract Google authenticator module under Home directory (assume you are already logged in home directory of root).

# cd /tmp
 # wget
 # bunzip2 libpam-google-authenticator-1.0-source.tar.bz2
 # tar xf libpam-google-authenticator-1.0-source.tar
 # cd libpam-google-authenticator-1.0
 # make
 # make install
 cp /lib64/security
 cp google-authenticator /usr/local/bin

Before configuring SSH, first set up Google Authenticator. Run “google-authenticator” as the user you wish to log in with via SSH. You will be prompted with a few questions.

Do you want me to update your "~/.google_authenticator" file (y/n) y|0&cht=qr&chl=otpauth://totp/user@server%3Fsecret%3DABCD12E3FGHIJKLMN
Your new secret key is: ABCD12E3FGHIJKLMN
Your verification code is 98765432
Your emergency scratch codes are:
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) y
If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

This requires all users to use Google Authenticator for SSH authentication. To only require those users with Google Authenticator configured for their account (the ~/.google_authenticator file exists), then instead enter “auth required nullok“.

The order in which you place items in this file matters. Given this configuration, you will first be prompted for your Google Authenticator verification code, then for your system account password when you SSH into the system.

Modify /etc/ssh/sshd_config. Verify these settings:

PasswordAuthentication yes
ChallengeResponseAuthentication yes
UsePAM yes
# service sshd restart

When you SSH into the system as a user configured for Google Authenticator, you will have to enter the verification code that is displayed in you Google Authenticator app, and then by your system password at the next prompt:

login as: root
Verification code: 01234567
Password: *******