SFTP服务配置笔记

本文介绍了SFTP的基本概念,强调其作为SSH的一部分,使用加密传输确保安全性。配置SFTP时,特别是针对root用户的限制,涉及到sshd_config的Subsystem和ChrootDirectory设置。文章列举了配置过程中遇到的问题,如无法登录、根目录更改导致的错误,以及文件夹权限对连接的影响。完整配置文件中展示了如何限制root用户并确保其他用户仍能SSH登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


大概做个笔记,大家有什么高见欢迎评论

SFTP基本概念

SFTP 是 SSH的一部分。在SSH软件包中,已经包含了一个叫作SFTP的安全文件信息传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作。SFTP使用了加密传输,所以速度相对较慢。

配置用户和根目录

以root用户为例。(系统中存在的用户皆可)
vi /etc/ssh/sshd_config

注释掉 Subsystem sftp /usr/lib/openssh/sftp-server
添加 Subsystem sftp internal-sftp

末尾添加
Match User root
  ChrootDirectory /mnt/Rainpoo
  #X11Forwarding no
  #AllowTcpForwarding no
  ForceCommand internal-sftp

重启服务生效:service sshd restart

如果要针对某个用户组生效,则将Match User root修改为Match Group 组名

存在问题

按如上修改后,ssh无法用root用户登录。会提示This service allows sftp connections only。因为这个问题对我没什么影响,大家可根据实际需要在配置文件中修改验证。

踩过的坑

1、我想修改根目录,于是将默认配置文件中的#ChrootDirectory none直接修改为ChrootDirectory /mnt,结果SFTP无法登陆,且ssh也无法登陆。还需要将 Subsystem sftp /usr/lib/openssh/sftp-server修改为Subsystem sftp internal-sftp才行,此时系统中的用户都可登录sftp

2、网上有说注释掉PermitRootLogin yes,root用户就能登陆,但是按照我上述的方式修改后,再将PermitRootLogin yes注释掉,sftpssh都无法登陆了。

3、不修改Subsystem sftp /usr/lib/openssh/sftp-server,且末尾不添加ForceCommand internal-sftp,连接失败,具体原因不明。(internal-sftp性能更好,在连接sftp的时候,系统不会fork出一个新的sftp进程)

4、文件夹权限导致连接失败。将/mnt权限设置为777,连接失败;改为755,连接成功(子目录权限也要改)。

完整配置文件

使用这个配置主要是避免所有用户都无法用ssh登录系统,方便调试

#	$OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile	.ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#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

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

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

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

Match User root
    ChrootDirectory /mnt
    X11Forwarding no
    AllowTcpForwarding no
    ForceCommand internal-sftp

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值