Linux系统上配置SFTP服务

SSH

  • SSH协议(Secure Shell,安全外壳)是一种需要进行加密和认证的,用于远程访问及文件传输的网络安全协议。
    SSH基于加密和认证的特性可以为用户提供更强大的安全保障机制,在用户使用不安全的网络环境登录设备时,SSH能够有效保护设备不受IP地址欺诈、明文密码截取等攻击。
  • SSH协议默认使用TCP 22号端口

SFTP

  • SFTP(SSH File Transfer Protocol)是一种基于SSH(安全外壳)的安全的文件传输协议,使用SFTP协议可以在文件传输过程中提供一种安全的网络的加密算法,从而保证数据的安全传输。
  • SFTP在Linux操作系统中,默认的端口客是22,传输提供了密码和密钥验证机制,可以有效防止传输过程的威胁和攻击。

配置SFTP

配置SFTP之前,首先要安装SSH,即安装OpenSSH。

安装OpenSSH

[root@localhost ~]# yum install openssh-server
Last metadata expiration check: 0:11:41 ago on Sun 05 Mar 2023 09:03:07 PM EST.
Package openssh-server-8.0p1-4.el8.x86_64 is already installed.
>>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<<
Upgraded:
  openssh-8.0p1-13.el8.x86_64                     openssh-clients-8.0p1-13.el8.x86_64             
  openssh-server-8.0p1-13.el8.x86_64             

Complete!
[root@localhost ~]# 

配置sshd_config配置文件

sshd_config文件一般在目录 /etc/ssh/ 下面。如下所示

[root@localhost ~]# cd /etc/ssh/
[root@localhost ssh]# ls -n *_config
-rw-r--r--. 1 0 0 1531 3月  16 2023 ssh_config
-rw-------. 1 0 0 3158 10月 23 16:33 sshd_config
[root@localhost ssh]#

打开文件如下:

# the setting of "PermitRootLogin prohibit-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# override default of no subsystems
#Subsystem      sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp

# Example of overriding settings on a per-user basis
Match User sftptest
X11Forwarding no
AllowTcpForwarding yes
PermitTTY no
ForceCommand internal-sftp
  • 注释掉 Subsystem sftp /usr/libexec/openssh/sftp-server 配置,然后在下一行写上 Subsystem sftp internal-sftp。注释行的意思是,需要使用一个可执行的文件sftp-server来开启SFTP服务,而新加的行是使用系统自带的internal-sftp来开启SFTP服务。我们这里使用系统内部自带的。
  • 然后Math user 后跟SFTP用户,即指定那个用户可以登陆SFTP服务,多个时,可以用用户组的方式,书写方式Math Group 用户组。sftptest是我新建的系统用户。
  • AllowTcpForwarding yes的意思是允许长连接,可默认,不用修改
  • ForceCommand internal-sftp 使用internal-sftp命令
  • 其它默认就可以了

以上就配置号了SFTP服务。

重启SSH服务

[root@localhost ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
[root@localhost ssh]# 

测试连接

[root@localhost ssh]# sftp [email protected]
([email protected]) Password: 
Connected to 127.0.0.1.
sftp> 

这样就连接上了。

注意,在新建用户后对于用户的根目录,或是指定的SFTP根目录执行修改权限的命令 chmod 755 根目录。有时这个也会引发无法登陆的问题。

使用另一个服务器上的FileZilla客户端进行连接测试

filezilla连接成功

好了,Linux服务器上配置SFTP服务就分享到这里,感谢翻阅,希望帮到你。

猜你喜欢

转载自blog.csdn.net/weixin_44131612/article/details/134013320