All Products
Search
Document Center

Elastic Compute Service:Change the default port used by an instance to accept connections

Last Updated:Aug 11, 2023

This topic describes how to change the default port used by an Elastic Compute Service (ECS) instance to accept connections.

Change the default port used by a Windows instance to accept connections

This section describes how to change the default port used by a Windows instance to accept connections. In this example, Windows Server 2012 is used.

  1. Connect and log on to the Windows instance.

  2. Change the value of the PortNumber registry subkey.

    1. Press Win+R to open the Run command window.

    2. Enter regedit.exe and press the Enter key to open the registry editor.

    3. In the left-side navigation pane, choose HKEY_LOCAL_MACHINE > System > CurrentControlSet > Control > Terminal Server > Wds > rdpwd > Tds > tcp.

    4. Find and right-click PortNumber in the right list, and select Modify....

    5. In the Edit DWORD (32-bit) Value dialog box, enter a different port number in the Value data field. In this example, enter 3399. Select Decimal in the Base section and click OK.

      api1
    6. In the left-side navigation pane, choose HKEY_LOCAL_MACHINE > System > CurrentControlSet > Control > Terminal Server > WinStations > RDP-Tcp.

    7. Find and right-click PortNumber in the right list, and select Modify....

    8. In the Edit DWORD (32-bit) Value dialog box, enter a different port number in the Value data field. In this example, enter 3399. Select Decimal in the Base section and click OK.

      api1
  3. Restart the instance in the ECS console.

    For more information, see Restart instances.

  4. Add security group rules to the security group of the instance to allow connections to the new port.

    For more information, see Add a security group rule.

  5. Connect to the instance. In the Remote Desktop Connection dialog box, enter <IP address of the instance>:<New port number> in the Computer field and click Connect to connect to the instance.

    远程访问服务器
    Note

    Mac Remote Desktop Connection can only be used to connect to the instance over the default port 3389.

Change the default port used by a Linux instance to accept connections

This section describes how to change the default port used by a Linux instance to accept connections. In the example, CentOS 6.8 and CentOS 7.7 are used.

  1. Connect and log on to the Linux instance.

  2. Run the following command to back up the sshd configuration file:

    cp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak
  3. Change the port number of sshd.

    1. Run the following command to edit the sshd_config configuration file:

      vim /etc/ssh/sshd_config
    2. Press the I key to enter Insert mode.

    3. Add a new port to accept connections.

      In this example, add port 1022. Enter Port 1022 under Port 22.新增端口

    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the file.

  4. Run one of the following commands to restart sshd. After sshd is restarted, you can log on to the Linux instance by using SSH port 1022.

    • If the Linux instance runs CentOS 7 or later or Alibaba Cloud Linux 2, run the following command:

      systemctl restart sshd
    • If the Linux instance runs CentOS 6, run the following command:

      /etc/init.d/sshd restart
  5. Add security group rules to the security group of the instance to allow traffic over TCP port 1022.

    For more information, see Add a security group rule.

  6. Use an SSH client to connect to the instance to check whether traffic over the new port is allowed.

    Enter the new port number in the Port field. In this example, enter 1022.输入修改后的端口号

    Note

    After the port number is changed, you cannot use the default port 22 to connect to the instance.