Post4VPS Forum | Free VPS Provider
How to change SSH port in VPS - Printable Version

+- Post4VPS Forum | Free VPS Provider (https://post4vps.com)
+-- Forum: VPS Discussion (https://post4vps.com/Forum-VPS-Discussion)
+--- Forum: Tutorials (https://post4vps.com/Forum-Tutorials)
+--- Thread: How to change SSH port in VPS (/Thread-How-to-change-SSH-port-in-VPS)

Pages: 1 2


How to change SSH port in VPS - AmirGT - 09-13-2017

Hello Folks. Last time I went to login to my VPS but I found a very weird thing, check the ping below.

[Image: 1505290836.png]

So someone was brute forcing my VPS and as you can see 22082 failed login so you have to take an action and do something before accessing your VPS so I found the solution for this and that solution was Changing SSH Port and that's going to be our tutorial for today.

So firstly you have to install nano using this command

On Ubuntu

Code:
sudo apt-get install nano


On CentOS and Fedora

Code:
sudo yum install nano


Then we have to type this command to edit the SSH settings [Works for all Linux Distributions]
Code:
nano /etc/ssh/sshd_config

Then you will find

Code:
#Port 22


You have to uncomment it and change the port, for example it's going to be like that

Code:
Port 55


Now save your changes and now we are done, last command we have to execute only to restart the SSH service.

Use this command if you are using Ubuntu

Code:
service ssh reload


And use this command if you are using CentOS or any Redhat Distribution

Code:
sudo systemctl restart sshd


Now we are done. Your connection will not be disconnected but next time you wanna to connect you have to put the port you chose in the port box (for me it was 55) like that
[Image: 1505293072.png]

And if you are using Linux so you have to type

Code:
ssh root@MyVPSIP -p 55
root = The VPS User
MyVPSIP = The IP of your VPS
-p = Means to use specific port
55 = Is the port I chose

The whole command means to connect to my VPS using a user called root in this IP and use specific port which it's 55.

That was our small tutorial for today, it seems too tall but it's so easy also I advise you to change the VPS port for not getting brute force or attacks on this port.
Thank you for reading my small tutorial, I hope you benefited from it and goodbye. Angel


RE: How to change SSH port in VPS - Liquidips - 09-13-2017

Depending of what Linux version you are using
you can do a search on google
but if you are using Cent OS Linux, try following the below instructions


1: Login with the root user name and password
-su

2: Backup the ssh config file
-sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

3: Edit the ssh config file
-sudo vi /etc/ssh/sshd_config

4: press insert key to begin the edit of the file
5: scroll down to #Port 22 - and enter the port number you wish you use - make sure to remove # after editing
6: type: ":wq" to save the changes and press: "ENTER" to confirm

7: Next install the python config
-sudo yum -y install policycoreutils-python

8: reenter the new port is here
-sudo semanage port -a -t ssh_port_t -p tcp "ENTER THE TCP PORT HERE FROM STEP 5"

9: Next check the firewall
-sudo firewall-cmd --permanent --zone=public --add-port="ENTER THE TCP PORT HERE FROM STEP 5"/tcp
-sudo firewall-cmd --reload

10: now restart the SSH services
-sudo systemctl restart sshd.service

11: Now the new port has been configured -
ss -tnlp | grep ssh

12: if you would like to rollback the change from steps 1 - 10
IN THE CASE SSH ISNT WORKING ANYMORE
you may need to Reinstall the SSH services - USING THIS COMMAND
-yum -y install openssh-server openssh-clients

Hope this help and please provide me your feedback.


RE: How to change SSH port in VPS - AmirGT - 09-13-2017

(09-13-2017, 11:38 AM)Liquidips Wrote: Depending of what Linux version you are using
you can do a search on google
but if you are using Cent OS Linux, try following the below instructions


1: Login with the root user name and password
-su

2: Backup the ssh config file
-sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

3: Edit the ssh config file
-sudo vi /etc/ssh/sshd_config

4: press insert key to begin the edit of the file
5: scroll down to #Port 22 - and enter the port number you wish you use - make sure to remove # after editing
6: type: ":wq" to save the changes and press: "ENTER" to confirm

7: Next install the python config
-sudo yum -y install policycoreutils-python

8: reenter the new port is here
-sudo semanage port -a -t ssh_port_t -p tcp "ENTER THE TCP PORT HERE FROM STEP 5"

9: Next check the firewall
-sudo firewall-cmd --permanent --zone=public --add-port="ENTER THE TCP PORT HERE FROM STEP 5"/tcp
-sudo firewall-cmd --reload

10: now restart the SSH services
-sudo systemctl restart sshd.service

11: Now the new port has been configured -
ss -tnlp | grep ssh

12: if you would like to rollback the change from steps 1 - 10
IN THE CASE SSH ISNT WORKING ANYMORE
you may need to Reinstall the SSH services - USING THIS COMMAND
-yum -y install openssh-server openssh-clients

Hope this help and please provide me your feedback.

Very nice but I have done all this in 3 commands only and no need to reinstall the SSH service or checking the firewall or anything else. Btw thank you for additional, my tutorial might doesn't work with some people so they may try yours. Big Grin


RE: How to change SSH port in VPS - sagher - 09-14-2017

Can you please tell also how to change Host/username of vps? and how to add custom domain name as a vps host name ?


RE: How to change SSH port in VPS - AmirGT - 09-14-2017

(09-14-2017, 04:16 AM)sagher Wrote: Can you please tell also how to change Host/username of vps? and how to add custom domain name as a vps host name ?

Do you mean VPS hostname which it's located inside the VPS like [root@amir ~] or the reverse DNS (rDNS)? Also do you mean to host a domain on the VPS right? Please explain more to be able to benefit you. Big Grin


RE: How to change SSH port in VPS - youssefbasha - 09-14-2017

(09-13-2017, 03:03 PM)AmirGT Wrote: Very nice but I have done all this in 3 commands only and no need to reinstall the SSH service or checking the firewall or anything else. Btw thank you for additional, my tutorial might doesn't work with some people so they may try yours. Big Grin

I think that he is checking if the someone who is trying to login made a thing and etc. 
And thats not bad as we can know if the person caused any diseases at the vps server.
Right? @Liquidips ?


RE: How to change SSH port in VPS - AmirGT - 09-14-2017

(09-14-2017, 11:11 AM)youssefbasha Wrote: I think that he is checking if the someone who is trying to login made a thing and etc. 
And thats not bad as we can know if the person caused any diseases at the vps server.
Right? @Liquidips ?

No, he just added an exception to the VPS firewall to allow him connecting the specified port which he chose above.


RE: How to change SSH port in VPS - sagher - 09-14-2017

(09-14-2017, 09:01 AM)AmirGT Wrote: Do you mean VPS hostname which it's located inside the VPS like [root@amir ~] or the reverse DNS (rDNS)? Also do you mean to host a domain on the VPS right? Please explain more to be able to benefit you. Big Grin

Yes the first one you explain . i go to hostname and edit it but i can't succeed. [root@amir ~]  which file i need to edit and which ssh command do my favor.


RE: How to change SSH port in VPS - AmirGT - 09-14-2017

(09-14-2017, 11:42 AM)sagher Wrote: Yes the first one you explain . i go to hostname and edit it but i can't succeed. [root@amir ~]  which file i need to edit and which ssh command do my favor.

You can simply change it from the VPS control panel you will fine an option called "Hostname" you can try it. Or you can try one of these tutorial
https://rimuhosting.com/howto/hostname.jsp
https://support.rackspace.com/how-to/centos-hostname-change/


RE: How to change SSH port in VPS - sagher - 09-15-2017

(09-14-2017, 01:12 PM)AmirGT Wrote: You can simply change it from the VPS control panel you will fine an option called "Hostname" you can try it. Or you can try one of these tutorial
https://rimuhosting.com/howto/hostname.jsp
https://support.rackspace.com/how-to/centos-hostname-change/

Service provider can not gives me control panel. thats why i rely on ssh putty.