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.

Grep Command Examples.

The grep is a Linux utility command which will help you in searching the contents within the file. It is the most useful when you are debugging through a large message file. It can be helpful in the scripts too.

Below are the few examples and the tricks for grep command.

Grep Command to highlight Color

Below command will search for “DB Error” in the /var/log/messages and highlight it with red color so you can quickly identify.

[root@server ~]# grep --color "DB Error" /var/log/messages

Print 3 lines before and after the pattern match in grep

If you want to print the lines before/after the pattern matches then you can use -A (after) or -B (before) option. Below command will print 3 lines before and after match.

[root@server ~]# grep "DB Error" /var/log/messages -A3 -B3

History Command Examples In Linux

To check history of the commands which has been executed.

[root@server ~]# history
    1  whoami
    2  clear
    3  ls
    4  whoaami
    5  ls -ltr
    6  top -c

To check last 5 commands in history use history 5

[root@server ~]# history 5
   92  clear
   93  whoami
   94  top -c
   95  history
   96  history 5

To clear history use history -c

[root@server ~]# history -c
[root@server ~]#
[root@server ~]# history
    1  history

To delete a particular command in history use history -d

[root@server ~]# history 5
1 history
2 whoami
3 clear
4 hisry 5
5 history 5

here I'm deleting the command which I fired at 4th Number
[root@server ~]# history -d 4
[root@server ~]# history
1 history
2 whoami
3 clear
4 history 5
5 history -d 4
6 history

To run a command at particular number in history you can use ! followed by the number. Example you want to run the command at number 2 the run !2

[root@server ~]# !2
whoami
root

TEE Command in Linux

TEE command is useful when you want to redirect the standard output to a file. Tee command should be already installed on your Linux Machine.

How to use Tee Command

[root@server ~]# cat file-1.txt | tee file-2.txt
a
b
c

This will overwrite all the contents of the file-2.txt with file-1.txt.
if want to keep the contents of the file-2.txt intact so you need -a option to append. As shown below.

[root@server ~]# cat file-1.txt | tee -a file-2.txt
one
word

Redirecting out of a command to a file. Most of us have habit of using >> to redirect output of a command to a file. But that’s work all the time where there is a error in the script.

So, to redirect output of a command to a file you need to use below command.
Example, my script has error but when I use >> to redirect output it won’t work.

[root@server scripts]# ./servicestatus.sh >> error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 0 May 29 06:05 error.log
[root@server scripts]#

My error log file is blank, so here I will use TEE command which works perfect. It will re-direct stdout and stderr into a file.

[root@server scripts]# ./servicestatus.sh 2>&1 | tee error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 66 May 29 06:08 error.log
[root@server scripts]#

[root@server scripts]# cat error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#