Disable direct root Login.

In previous post we saw how to Set Alerting for root login and How to Change Default SSH Port. To add more to the security you should also disable direct root Login and specific users should be allowed only.

To disable Direct root Login, you need to follow the steps below.

1. Open the file /etc/ssh/sshd_config and add the line below.

PermitRootLogin no

2. Create a new user – e.g. user bob and set a password to that user.

3. Open file /etc/group and add the user to the Wheel group. So the Wheel group in that file would look like this.

[root@server ~]# cat /etc/group | grep -i wheel
[root@server ~]#

4. Finally restart sshd service.

[root@server ~]# systemctl restart sshd

5. Please make sure that permissions to below are set correctly.

chmod 4755 /bin/su
chmod 1700 /etc/passwd
chmod 1700 /etc/shadow
chmod 1755 /etc/groups

Remember the Golden Rule? Yes, always verify the changes from a different session before logging out the current one.

Set alerting for root login.

If you are using Linux server, you must have enhanced security by Changing Default SSH port. (If you haven’t you should do it immediately). To further secure your environment, you should consider setting alerts for root login.
Setting alerts for root account login is useful in many ways. To track who logged in and for security reason,

Steps to enable Alerting for root login

  1. Login to server and switch to root user.
  2. Open file /root/.bash_profile and append the code below.
/usr/bin/echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" email@yourdomain.com

Simple, isn’t it?
You can try to login/switch to root user and you should get the email like this.

ALERT - Root Shell Access on: Thu Jul 23 09:42:46 UTC 2020 user pts/0 2020-07-23 09:42 (xx.xx.xx.xx)

Basic Openssl Commands

Openssl is an open source implementation SSL protocol. It is widely used and it has different functions which allows you to check CSR, Certificate and it’s expiry.

Here are some basic open commands which will be useful when you are dealing with certificate or anything related to that.

To decode certificate.

[root@server ~]# openssl x509 -in certificate.crt -text -noout

Check Expiry Date of Certificate Using OpenSSL command

[root@server ~]# cat certificate.crt | openssl x509 -noout -enddate

How to see if Certificate and a Private Key match.
Compare the output from both commands. If they are identical then the private key matches the certificate.

openssl pkey -in privateKey.key -pubout -outform pem | sha256sum
openssl x509 -in certificate.crt -pubkey -noout -outform pem | sha256sum 

Above are the most basic Operations and Widely used. Do let me know if you want us to add more here. So you can refer here.

How to change Default SSH port

Why should you change default SSH port in Linux?

The main reason to change the default SSH Port from 22 to something else is to prevent server from SSH attacks. Port 22 is default SSH port and that is the first thing it would be tried. Changing default port will prevent your server from brute-force and malicious attacks.

How to change SSH port in Linux

To change SSH port you will need to have root access to the system. The first Step would be choosing the port. You should always avoid ports from 0-1023 as they are system default ports.

Open file /etc/ssh/sshd_config

[root@server ~]# vi /etc/ssh/sshd_config

Looks for the line below

#Port 22

Now remove comment and change the port, so the line should look something like this.

Port 5867

Now you will need to restart sshd service so the changes would be reflected.

[root@server ~]# systemctl restart sshd

Important Note: Before you change port please make sure you have whitelisted the new port in the firewall, else you will be locking your self out.

Testing the Changes

Well, the golden rule is to verify all the changes you do and make sure it works. So before logging out the current session, you will have to make sure that you are able to access the server using new port.

[root@server ~]# ssh -p 5867 username@your-server-ip

Done, you have successfully updated SSH port. Please do always remember to allow new port in firewall and to test new port before closing the current session – so that you don’t lock out.