How do I modify the default SSH port in the CentOS system?

Source: Internet
Author: User
Tags exit ssh centos vps iptables port number ssh access ssh port

How do I modify the default SSH port in the CentOS system?

The default CentOS default port is 22, and you'll find a lot of log files in a long time, which is the error log left by the scan attack. So what we're going to do today is to change the number that we know, which reduces the probability of being scanned, and it increases security.

1, login ssh Modify configuration file, enter the following command

Vi/etc/ssh/sshd_config

The following prompts are available, and you can see #port 22, which represents the default port 22. Press the A,VI command of the keyboard A is an editor, you press a after entering the edit state, this time you can use the keyboard keys to move the green cursor and the keyboard delete key to remove the front # number.

Tip: Why do you want to remove the # 22 front of the port first? Because in the configuration file, # is a Linux annotation, no code will ever be executed. The SSH default port is 22, so the configuration file appears as # by default. When you need to change to another port or add multiple ports while SSH access, you should delete the annotation symbol, so that the program can execute your command.

2, the following figure press ENTER another line manually add 12366 ports (custom port recommended to select 5-digit port, such as: 10000-65535), keep 22 is to prevent the firewall shielding other ports caused unable to connect to the VPS, such as you commented on Port 22, The newly added 12366-Port firewall is not released, so congratulations on digging your own hole, maybe next time you won't be able to connect to the VPS via SSH. In order to prevent this embarrassing situation, we should leave a path, wait until the new port can be normal connection ssh, and then delete 22 port is not too late.

Press the "ESC" key to exit the current state, and then enter the ": Wq" command to save the exit.

3, after the above modifications, restart the SSH service and exit the currently connected SSH port. The order is as follows:

Service sshd Restart

4, SSH software inside the 22 port into 12366 ports and then connect the VPS try. In Xshell, click on the file, open, pop-up "session" window, modify the port number of 12366.

Then save, connect, this time the discovery of sad reminders not even on the VPS, analysis is because did not add 12366 ports to the VPS inside the firewall caused, fortunately just left the back door! So repeat the fourth step above to change the Xshell port number back to 22, landing on the VPS.

5, now to do is to add 12366 ports to the firewall list. The VI Editor opens the list of firewall commands as follows:

Vi/etc/sysconfig/iptables

Press A on the keyboard, enter the edit state of the VI command, enter another line using the up and down key and delete key to add 12366 port. You don't need to know what those commands mean, just type the 22 port as it is, and change the port from 22 to 12366, and note that Linux is case-sensitive.

After adding a new port, press ESC to exit the current state, and then enter: Wq to save the exit.

6. After modifying the firewall list, you have to restart the firewall, as follows:

Service Iptables Restart

7, now enter the firewall Status View command as follows

Service Iptables Status

The following figure firewall rules already have 12366 ports.

8, now repeat the above 4th step inside the Modify SSH software port steps, to 12366 ports, connect VPS. Pop-up prompts select "Accept and save", and then connect to the VPS will not have this hint.

When the new port can log in normally, repeat the first step above, remove the rear door 22 port just left. Then restart SSH as instructed in the third step above.

Today's topic is to modify the CentOS default port, but there are many commands involved, the following summary.

VI Editor Simple application, A is editor,: Wq is save and exit, the keyboard "ESC" button is out of the current state. The keyboard's up-down and delete keys are added.

Edit SSH configuration file command: Vi/etc/ssh/sshd_config

Reboot SSH command: service sshd restart

View firewall Status command: Service iptables status

To edit a list of firewall commands: Vi/etc/sysconfig/iptables

Reboot Firewall command: Service iptables restart

You need to be aware that Linux is case-sensitive when you enter commands.

Weies Blog hope that you can not only learn how to modify the CentOS port method, but also to understand the relevant commands, but also familiar with the Linux VI Editor's simple command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.