Change SSH Port

Imran Afzal (250k+ students)
A free video tutorial from Imran Afzal (250k+ students)
Bestselling Instructor with over 250,000 students worldwide
4.6 instructor rating • 16 courses • 247,152 students

Lecture description

Change SSH port number 22:

  • Open the /etc/ssh/sshd_config file

  • replace default Port 22 with different port number say 1110

  • save & exit from the file

  • service sshd restart

  • ssh username@IP -p 1110

Learn more from the full course

Complete Linux Security & Hardening with Practical Examples

The Best Linux Security Course that prepare you to protects your Systems from attacks by hackers. Helps in RHCSA & RHCE

16:14:33 of on-demand video • Updated January 2021

  • By the end of this course you will be able to apply most of the security measures in your Linux environment
  • You will be able to pass the Redhat Certified Engineer exam (EX300)
  • You will reduce the risk of your Linux system being hacked or attacked
  • You will have expert knowledge of Linux account and system securities
English [Auto] This lecture is about changing SSA port on your Linux machine. Now let's first take a quick review how a server connects from one server to another. Well of course you know that connects from IP to IP. If your server has an IP and remorse over that you're trying to connect has an IP but it doesn't end right here. Every server there runs services to listen it has a predefined ports so to connect from any Unix like operating system to another Eunice Unix like operating system. You have to connect through SSD sake and the SSA each port runs on Port 22 and that is by default. That is out of the box that support you get. And if you're running telnet you'll see it is running on Port 23 anyway telnet should not be using at all because it's not secure at all. If you're using 10 that you should stop it right away. So anyway SSA port number two is the one that is used to connect front to a Linux machine and you could get a listing of all the ports in at sea services. So by default all the application of programs or services that are run in your Linux machine has a predefined port a tash to them you could change them but by default they come in as a predefined and who comes up with those ports. Well there is I E pub you heard of that organization they are the one who would define which port connects to which servers and so on. But anyway we do want to get into that. We just wanted to make sure how we could change that default port 22 to a different port. Let me get into a more figurative way so you will understand exactly what this whole lecture is about. So there is a server a and that server wants to connect to server B now server a will use some kind of a middle party terminal like putty or if its a Windows machine. And it's trying to connect to another vendor's machine it will use something like remote desktop connection. Now it has the built in algorithm like putty has that runs on Port 22. If you open up a party you will see a you put in the IP address right next to it. It has a port number predefined for you which is 22 you could change that if you have changed it to the remote server anyway so that you connect using the IP and the port number and then you send the request to the remote server server be now server B says Hey I got the request and you are asking me are are trying to connect to me using port 22. Let me first see if I have a listening SSD HD service running that is listening on Port 22 and it says yes I do have SSD age D running and it is running on Port 22 perfect. And go ahead and create it and establish a session. Perfect session is establish everything works. Everything's hunky dory. Perfect. Now what happened when a hacker comes into the equation a hacker wants to get into server B. Now of course he wants to get into every which way. So he usually would hackers do they have a pre written script or executable that they have which actually attacks all the ports and whichever ports are open they are in luck and they will attack that port. So that tack goes out and attack says hey I am trying to get to these poor 22 53 and so on whichever the servers those ports belong to. So now if you had changed that port from 22 to let's say SSA de change port to 1 1 0 then you would have prevented that attack completely. See that's how you could changing you could change the port and make your Linux server more secure. Let's look at how we can make that change in our next machine first of all then make a backup of a configuration file and the configuration file is at CSC SSA D underscore config that's where we go in and change the port number then we modify that file and we change the parameter that says Port 22 change to port 1 1 1 0 by the way 1 1 1 0 is just an arbitrary number or any number that I've picked. But make sure you are not picking the port that is being used by other services because it's going to cause a conflict to find out which port is available you could go to at sea services file and if we have a port that you'd want to assign to port SS sage then you could grep for that port and make sure it is not in use. Anyway I have checked that already the 1 1 1 0 is not signed or not associated with any services I'm going to use that port just for this practice or this exercise or for this lecture. Now once that is done I will save the file and restart the server or restart the service and restart the services systems TTL restart SS h d by the way I had a single code at the beginning which somehow got deleted. But anyway you don't need single double code. So at the end of this stage do you see double code. You could just ignore that. Then from Unix like OS connect to using comment in text like a each user name at IP dash P for port and define the port number. Now I'll go into my linux machine and we could try that on Linux machine and see how it works. So right now first if you see I'm going to open up a new session and you see right here if I put an IP address one 90 to 168 one dot com let's see what is my IP address my IP address 1 I do 161 dot 36 so 36 and you see by default it's giving me port 22 and when I click open it should allow me to connect to see I'm connected. Perfect. Now I wanted to change that port 22 so let me close that and I will go first make up backup copy has cleared the screen copy at C Ss sage SSA D underscore config and I will make a copy and I'll put it somewhere like in temp SSA which underscore config and I'll make it orange it is copied perfect. If I make a mistake I could always revert back to this file. Also you could make a snapshot of your Linux machine as well. In case you cannot get in and you want to revert back. So now I have made a copy. Perfect Now I will modify the SSA D and score config file so I'll go to at Etsy SSA H and VII SS HD underscore config and I'll go to the line where it says Port 22. I'm not going to modify that because it's already commented out what I'll do is I'll add another line by heading 0 and I'll put P O R T P make sure is uppercase I'll put it port 1 1 1 0 that's it. Save it. And it will take effect when you restart the service system CTO I'll restart SSA sage D. That said it restarted. Now when I tried to connect new session load and then I tried to connect to this machine using port twenty two let's see what happens. Open Sea network our connection refused Y because the SSA D is no longer listening on Port 22. So that's how you could avoid the attack. Now if you wanted to do the same thing and you do new session and you do load the service and change the port 1 1 1 0 and do open and there you go. Now I am able to connect and it is working as expected. So that's how you could avoid the attack and how you can make your system secure.