Random Linux

Linux, video games and web hosting

To change your SSH port on CentOS you can first edit the sshd_config file located in /etc/ssh/sshd_config.

nano /etc/ssh/sshd_config

Once you open that up the port line should be commented out and look something like this:

#Port 22

So you can uncomment it and set it to whatever port you’d like…I’ll use 2222 in this example.

Port 2222

Once you do that go ahead and save it by hitting CTRL+O and enter, then CTRL+X to exit nano. Now we need to open up the new port in iptables by running these commands:

iptables -I INPUT -p tcp --dport 2222 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart

Now that port 2222 is open and accepting connections, we can go ahead and restart the sshd service.

/etc/init.d/sshd restart

Once you do this go ahead and open a new terminal or PuTTy session and try to connect to the new port to test that it’s working while leaving your original session open in case it does not. If it doesn’t work please be sure you followed all the instructions carefully because if you close your first session you could get locked out of your server!

January 16th, 2011

Posted In: How To


Leave a Reply

Your email address will not be published. Required fields are marked *