• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Configure Separate Port For SSH and SFTP On CentOS/RHEL

by admin

Sometimes for the security reasons, there is a need to have a separate port for ssh and sftp on CentOS/RHEL. Here’s a short how-to on configuring SSH on port 2222 and SFTP to work on port 22.

1. Since openssh-5.3p1-117.el6.x86_64 for separating the ssh and sftp port on the CentOS/RHEL systems, make changes to the /etc/ssh/sshd_config file and add the following modifications.

# vi /etc/ssh/sshd_config
Port 22
Port 2222

2. Comment the default subsystem and add the below subsystem.

# vi /etc/ssh/sshd_config
Subsystem sftp internal-sftp

Add the below lines at the end of file /etc/ssh/sshd_config

# vi /etc/ssh/sshd_config
Match LocalPort 22
AllowTCPForwarding no
X11Forwarding no
ForceCommand internal-sftp

3. After making the modifications just save and restart the sshd service.

# service sshd restart

This will allow port 22 for sftp connections only. Port 2222 can be used for ssh to the server.

Verify

Let’s verify our configuration. First, try ssh to the server with default port (22). It should fail as shown in the example below:

# ssh root@192.168.10.12
root@192.168.10.12's password:
This service allows sftp connections only.
Connection to 192.168.10.12 closed.

Now try ssh by providing the port 2222 as well. It should succeed this time.

# ssh -p 2222 root@192.168.10.12
root@192.168.10.12's password:
Last login: Mon Apr 17 15:23:58 2018 from ......

As a last verification, try sftp to the system with default port i.e. 22:

# sftp root@192.168.10.12
Connecting to 192.168.10.12...
root@192.168.10.12's password:
sftp>

Filed Under: CentOS/RHEL 6, Linux

Some more articles you might also be interested in …

  1. How to Use iptables instead of firewalld on CentOS/RHEL 7 and 8
  2. dconf Command Examples in Linux
  3. Echo Command with Practical Examples
  4. Understanding The /proc File System
  5. Understanding How Umask Controls the Initial File / Directory Permissions in Linux
  6. kdialog: command not found
  7. cmus : command not found
  8. How to Reserve a Port Range for a Third Party Application in CentOS/RHEL
  9. ddcutil: command not found
  10. CentOS / RHEL : How to rotate /var/log/wtmp and /var/log/btmp file using logrotate

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright