Linux Modify SSH Port

Source: Internet
Author: User
Tags ssh port

The tutorial is currently the safest tutorial, if you are a novice please follow the steps of the tutorial strictly, if there is a certain basis for people can selectively skip certain paragraphs

Modify Port Configuration

Run first

    1. Vim/etc/ssh/sshd_config


Find #port 22 This line and then remove the previous comment, then add another line to Port 1234

Port 1234

    1. Many tutorials are directly modified 22 for other ports, it is best not to do so, in case the modified port can not be used, you will not be able to log on, so first left a posterior


Modify firewall configuration


Modify the Iptables (Linux firewall) file:

    1. Vim/etc/sysconfig/iptables


Add a row

Iptables-a input-m State--state new-m tcp-p TCP--dport 1813-j ACCEPT


Shutting down the firewall

Then shut down the firewall

    1. Service Iptables Stop


Turn off SELinux

Important: turn off SELinux first, many tutorials do not mention this, because SELinux default protection level is very high, if you do not close selinux there will be a lot of problems

    1. Vim/etc/sysconfig/selinux


Change the selinux=enforcing line to selinux=disabled and restart

Test connection

After reboot, run the command:

    1. NETSTAT-TPNL | grep ssh

You'll see two ports open at the same time.

  1. [[email protected] ~]# netstat -tpnl | grep ssh  tcp         0      0 0.0.0.0:22                   0.0.0.0:*                     listen      1497/sshd              tcp        0       0 0.0.0.0:1234                  0.0.0.0:*                    LISTEN      1497/sshd          &nbsP;   tcp        0      0  :::22                        :::*                          LISTEN       1497/sshd             tcp         0      0 :::1234                        :::*                          listen      1497/ Sshd


Important : Then test whether the new port can be connected through

Modifying the SELinux configuration

If normal, proceed with the following steps: Modify SELinux

Just installed CentOS is no semanage command, so please confirm the existence of semanage command, if not, follow the following tutorial installation

  1. # rpm -qa | grep semanag  libsemanage-2.0.43-4.2.el6.x86_64     # which semanage  /usr/bin/which: no semanage in  (/usr/ Lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)      # yum provides /usr/sbin/semanage  policycoreutils-python-2.0.83-19.30.el6.x86_ 64 : selinux policy core python utilities  repo         : base  Matched from:  Filename     : /usr/sbin/semanage    # yum whatprovides /usr/sbin/semanage   loaded plugins: fastestmirror, refresh-packagekit, security  loading  mirror speeds from cached hostfile   * base:  Mirrors.prometeus.net   * epel: mirror.switch.ch   * extras: mirrors.prometeus.net   *  rpmfusion-free-updates: rpmfusion.famillecollet.com   *  rpmfusion-nonfree-updates: rpmfusion.famillecollet.com   * updates:  mirrors.prometeus.net  policycoreutils-python-2.0.83-19.30.el6.x86_64 : selinux  policy core python utilities  repo        :  base  matched from:  filename    : /usr/sbin/semanage     # yum -y install policycoreutils-python


When you are finished, use the following command to view the SSH ports currently allowed by SELinux:

    1. Semanage Port-l | grep ssh


Will output

    1. ssh_port_t TCP 22


Add 1234 port to SELinux

    1. Semanage port-a-t ssh_port_t-p TCP 1234


Important: then make sure you add it.

    1. Semanage Port-l | grep ssh


If successful will output

    1. ssh_port_t TCP 1234, 22


Turn on SELinux lower security level

Then go to modify the SELinux policy, reopen it, but we first modified to permissive this security level will not intercept your changes, just prompt warning

    1. Vim/etc/sysconfig/selinux

To modify the security level

    1. Selinux=permissive


Then reboot reboot.

important : Once again, test the remote connection with the new port after reboot.

Turn on SELinux normal security level

Test the connection with the new port after rebooting.

If the test is modified by modifying the selinux=enforcing and then restarting, because there are many changes in the way mentioned earlier, this side will not be affixed to the code

Test remote connection with new port again after reboot

Subsequent cleanup steps

If you can, congratulations, the successful configuration of the new port, and then you can do it yourself to remove the 22 port from the Sshd_config, the 22 port removed from the firewall and so on steps


Linux Modify SSH Port

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.