---
title: 'How to change the SSH connection port disable password authorization'
description: 'In this article, we will see how to change the standard SSH connection port to a non-standard one, and disable password authentication.'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/07/01/1658914972_2.png'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [FAQ](https://zomro.com/blog/faq/)
  * How to change the SSH connection port and disable password authorization 



# How to change the SSH connection port and disable password authorization

16-07-2022

In this article, we will see how to change the standard SSH connection port to a non-standard one, and disable password authentication.  
  
**!!! IMPORTANT !!!**  
  
**Make sure you have already created SSH keys before doing this, and successfully connected to the server using them. Or you have access to the server via VNC to enable password authorization back and return the default settings.**  
  
And so, first you need to edit the **sshd_config** file:  

    
    
    vim /etc/ssh/sshd_config

  
In the line **Port 22**(Uncomment if commented out) and change port **22** to the desired port, for example **22333** :
    
    
    Port 22333

  
![How to change the SSH connection port and disable password authorization - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/07/01/1658914972_2.png)  
Restart the **sshd** service:
    
    
    systemctl restart sshd

  
Check if the installed port **22333** is listening:
    
    
    netstat -tupln | grep ssh

  
![How to change the SSH connection port and disable password authorization - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/07/01/1658915028_4.png)  
  
If we see that the **sshd** service listens to the port we need 22333 or another one that you set, then in order for us to connect already using it, you need to allow its use from outside.  
  
To do this, you need to add a rule to the iptables list:
    
    
    iptables -A INPUT -p tcp --dport 22333 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

  
or like this:
    
    
    iptables -I INPUT -p tcp --dport 22333 -m state --state NEW -j ACCEPT

  
With the **iptables -L** command, we can see that the rule has been added.  
  
![How to change the SSH connection port and disable password authorization - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/07/01/1658914616_screenshot_1.png)  
After that, you need to save the rules so that after the restart the installed port remains available.  
  
For **Centos 7** use this command:
    
    
    service iptables save

  
For **Ubuntu** , we use a different command. To do this, you need to install **iptables-persistent**.
    
    
    apt-get install iptables-persistent

  
If you get an error that there is no suitable package or repository, update the repositories. After that, install the updates.
    
    
    apt update
    apt upgrade

  
If this package is installed and you want to save the rules:
    
    
    dpkg-reconfigure iptables-persistent

  
During reconfiguration, answer **YES** to both questions.  
  
After that, you can save the rules for **ipv4** :
    
    
    iptables-save | sudo tee /etc/iptables/rules.v4

  
For **ipv6** :
    
    
    ip6tables-save | sudo tee /etc/iptables/rules.v6

  
If the firewall is enabled, add a rule for it:
    
    
    firewall-cmd --permanent --add-port=22333/tcp

  
And also restart the **firewalld** service:
    
    
    systemctl restart firewalld

**Disable SSH password authentication**

  
To do this, edit the **/etc/ssh/sshd_config** file:
    
    
    vim /etc/ssh/sshd_config

  
Looking for the line:
    
    
    PasswordAuthentication yes

  
![How to change the SSH connection port and disable password authorization - 4](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/07/01/1658915046_5.png)  
And change it to:
    
    
    PasswordAuthentication no

  
If there is a # symbol (commented out) at the beginning of this line, remove it. Save the file after making these changes and restart the **sshd** service:
    
    
    systemctl restart ssh

  
Now you can restart the server and make sure you did everything right.
    
    
    reboot

  
  
If everything is done correctly, then the connection to the server with the new port will pass without "dancing" incidents.

Similar articles:

[Create an FTP user in Hestia CP and ISPmanager](https://zomro.com/blog/faq/686-create-an-ftp-user-in-hestia-cp-and-isp-manager) [How to create your own Windows image for Cloud VPS (OpenStack)](https://zomro.com/blog/faq/513-how-to-create-your-own-windows-image-for-cloud-vps) [UFW firewall setup on Ubuntu 20.04](https://zomro.com/blog/faq/343-ufw-firewall-setup-on-ubuntu-2004) [How to install HestiaCP](https://zomro.com/blog/faq/245-kak-ustanovit-hestiacp) [How to allow images larger than 2560 pixels to be uploaded in WordPress](https://zomro.com/blog/faq/362-how-to-allow-images-larger-than-2560-pixels-to-be-uploaded-in-wordpress)

[ GPU server from €174.80/mon ](/dedicated-servers/type=gpu)
