Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Change the SFTP Port - DesignLinux

designlinux 0 Comments

SFTP (SSH File Transfer Protocol) is a secure file protocol for transferring files between two hosts over an encrypted connection. It also allows you to perform various file operations on remote files and to resume file transfers.

SFTP can be used as a replacement for the legacy FTP protocol. It has all the functionality of FTP but with a more secure connection.

This article explains how to change the default SFTP port in Linux. We will also show you how to configure your firewall to allow on the new port.

Don’t confuse SFTP with FTPS. Both protocol serve the same purpose. However, FTPS stands for FTP Secure, and it is an extension to the standard FTP protocol with support for TLS.

What Port Does SFTP Use #

SFTP is a subsystem of SSH and provides the same level of security as SSH.

The default SFTP port is 22.

Changing the SFTP Port #

Changing the default SFTP/SSH port adds an extra layer of security to your server by reducing the risk of automated attacks.

The best way to protect your server from attacks is to configure your firewall to allow access to port 22 only from trusted hosts and set up an SSH key-based authentication .

The following steps describe how to change the SSH Port on Linux machines.

1. Choosing a New Port Number #

In Linux, port numbers below 1024 are reserved for well-known services and can only be bound to by root. Although you can use a port within the 1-1024 range for the SSH service to avoid port allocation issues, it is recommended to choose a port above 1024.

This example shows how to change the SFTP/SSH port to 4422, but you can choose any port of your liking.

2. Adjusting Firewall #

Before changing the SFTP/SSH port, you’ll need to open the new port in your firewall.

If you are using UFW, the default firewall in Ubuntu , run the following command to open the port:

sudo ufw allow 4422/tcp

In CentOS, the default firewall management tool is FirewallD. To open the port, enter the following commands:

sudo firewall-cmd --permanent --zone=public --add-port=4422/tcpsudo firewall-cmd --reload

CentOS users also need to adjust the SELinux rules to allow the new SSH port:

sudo semanage port -a -t ssh_port_t -p tcp 4422

If you are using another Linux distribution that runs iptables, to open the new port run:

sudo iptables -A INPUT -p tcp --dport 4422 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

3. Configuring SFTP/SSH #

The SSH server configuration is stored in the /etc/ssh/sshd_config file. Open the file with your text editor:

sudo vim /etc/ssh/sshd_config

Search for the line starting with Port 22. Typically, this line is commented out using the hash (#) symbol. Remove the hash # and enter your new SSH port number:

/etc/ssh/sshd_config
Port 4422

Be very careful when editing the configuration file. An incorrect configuration may prevent the SSH service to start.

Once done, save the file and restart the SSH service for changes to take effect:

sudo systemctl restart ssh

In CentOS the SSH service is named sshd:

sudo systemctl restart sshd

Verify that SSH daemon is listening on the new port:

ss -an | grep 4422

The output should look something like this:

tcp   LISTEN      0        128            0.0.0.0:4422           0.0.0.0:*
tcp   ESTAB       0        0      192.168.121.108:4422     192.168.121.1:57638
tcp   LISTEN      0        128               [::]:4422              [::]:*

Using the New SFTP Port #

To specify the port number invoke the sftp command with the -P option followed by the new port number:

sftp -P 4422 username@remote_host_or_ip

If you are using a GUI SFTP client, simply enter the new port in the client interface.

Conclusion #

The default SFTP port is 22. However, you can change the port to whatever number you want.

If you are regularly connecting to multiple systems, you can simplify your workflow by defining all of your connections in the SSH config file .

Feel free to leave a comment if you have any questions.

sftp ssh security

Related

Tags: security, sftp, ssh

How to Extract (Unzip) Tar Bz2 File

Prev Post

How to Install Memcached on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,091
  • 571,446

DesignLinux.com © All rights reserved

Go to mobile version