简体   繁体   中英

Change the default port(22) SSh [CENTOS]

I am trying to change d default port (22) for my system, I edited the sshd_config file by changing to my desirable port no (5555) and restarted my service but it seem not to work . is there something else i have to change? thanks

Yes, you must allow that new port in iptables. iptables is the firewall program for Linux.

With root privileges you will need to do this:

iptables -I INPUT 1 -p tcp  --dport 5555 -j ACCEPT  

Make sure you can connect using port 5555 then disable the old port.

iptables -A INPUT -j DROP -p tcp --dport 22

then save the new settings

/etc/init.d/iptables save

Check out this really good webpage for more information:

http://www.rackspace.com/knowledge_center/article/introduction-to-iptables#Save_Save_Save_your_Ruleset

1) Make a backup of the sshd config file (optional but a good idea):

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

2) edit the sshd_config file to include your alternate port

vi /etc/ssh/sshd_config

Leave the default port 22 active for now and add your custom port

# Open ports for sshd
Port 22
Port 5555

3) Open the custom port in your firewall

iptables -I INPUT 1 -p tcp  --dport 5555 -j ACCEPT  
service iptables save

4) Use a new terminal window to make sure you can login with your custom port

ssh -p 5555 myuser@myserver.com

If this doesn't work, now you can still login on port 22 to troubleshoot

5) Once you've confirmed the custom port works, edit the sshd_config file and firewall settings to block port 22

vi /etc/ssh/sshd_config

...

# Open ports for sshd
# Port 22
Port 5555

...

iptables -A INPUT -j DROP -p tcp --dport 22
service iptables save

Now you'll want to test again to make sure you can login with your custom port but not with the default port of 22.

You will need to have root privileges or use sudo for the above commands.

Step - 1:

  nano /etc/ssh/sshd_config

and change the port from 22 to the desired one ie 5555

  service sshd restart

Step - 2:

 nano /etc/sysconfig/iptables

Here, you will find an entry for port 22. You will need to change it to 5555

  service iptables restart

Now, try the SSH using,

 - ssh -p 5555 root@Your IP Address

To Change the SSH Port for Your Linux Server

  1. Connect to your server via SSH

  2. Switch to the root user

  3. Run the following command:

     vi /etc/ssh/sshd_config 
  4. Locate the following line: #Port 22

  5. Remove # and change 22 to your desired port number like 2224 etc.

  6. Restart the sshd service by running the following command:

     service sshd restart 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM