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)

Echo Command in Linux

Echo command mainly prints on the screen whatever you ask it. It a simple function but most the scripts would be incomplete with echo command. You won’t be able get visible output from the shell script, without echo command.

How to check what version of echo command you are using?

[root@server ~]# /bin/echo --version
echo (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Brian Fox and Chet Ramey.

How to print using echo command ?

it’s actually quite simple. Just use echo command following by the text.

[root@server ~]# echo "Hello JustGeek"
Hello JustGeek
[root@server ~]#

Printing Variables.

it’s not just for the print text there are many things you can do using echo command. Example you can print Variables.

[root@server ~]# IamVariable="This is JustGeek"
[root@server ~]# echo $IamVariable
This is JustGeek
[root@server ~]#

You can also run a command through echo command and print it’s output to the screen.

[root@server ~]# echo "This server is online from $(uptime | awk '{print $3,$4}')"
This server is online from 77 days,

Formatting using Echo Command.

To use formatting functions with echo -e. It enables interpretation of backslash escapes.
eg:- “\n” parameter will print on the new line. As shown below.

[root@server ~]# echo -e "First Line\nSecond Line"
First Line
Second Line
[root@server ~]#

There are multiple characters which can be used which suits you.

\a: Alert (historically known as BEL). This generates the default alert sound.
\b: Writes a backspace character.
\c: Abandons any further output.
\e: Writes an escape character.
\f: Writes a form feed character.
\n: Writes a new line.
\r: Writes a carriage return.
\t: Writes a horizontal tab.
\v: Writes a vertical tab.
\\: Writes a backslash character.

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.