目录
一、规划
此方案规划22端口只能登陆不能上传下载文件,10022端口只能上传下载文件不能登陆,都使用root用户,上传下载文件不限制存储目录 。
二、配置服务
2.1 相关配置文件分离
#复制ssh服务相关文件给sftp一份
cp /etc/pam.d/sshd /etc/pam.d/sftpd
cp /etc/sysconfig/sshd /etc/sysconfig/sftp
#相关命令做软连接给sftp使用
ln -sf /usr/sbin/service /usr/sbin/rcsftpd
ln -sf /usr/sbin/sshd /usr/sbin/sftpd
2.2配置sftpd.service系统文件
cat > /etc/systemd/system/sftpd.service << EOF
[Unit]
#修改描述
Description=sftpd server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
#修改环境变量文件
EnvironmentFile=/etc/sysconfig/sftp
#修改sftp服务启动命令
ExecStart=/usr/sbin/sftpd -f /etc/ssh/sftpd_config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
EOF
2.3修改sftp配置文件
cat > /etc/ssh/sftpd_config << EOF
# ====== SFTP 传输配置 (10022端口) ======
Port 10022
Protocol 2
# 基础设置
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTH
LogLevel VERBOSE
LoginGraceTime 30
# 安全设置
PermitRootLogin yes
StrictModes yes
IgnoreRhosts yes
PermitEmptyPasswords no
X11Forwarding no
# 认证设置
PubkeyAuthentication yes
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication no
UsePAM yes
# 禁用所有非 SFTP 功能
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
PermitUserEnvironment no
# 强制仅使用 SFTP
Subsystem sftp internal-sftp
ForceCommand internal-sftp # 全局强制执行 SFTP
# 禁用所有 shell 访问
Match All
AllowAgentForwarding no
AllowTcpForwarding no
PermitTTY no
X11Forwarding no
EOF
2.4 创建 /var/run/sftpd.pid文件
touch /var/run/sftpd.pid
2.5取消22端口的sftp功能
vim /etc/ssh/sshd_config
#注释掉开头是Subsystem sftp的这一行即可,例如下面
#Subsystem sftp /usr/libexec/openssh/sftp-server
#另外增加一行,确保22端口只能登录
ForceCommand /bin/bash
2.6重启sshd服务
systemctl daemon-reload
systemctl restart sshd
2.7启动sftp服务
systemctl daemon-reload
systemctl start sftpd.service
systemctl enable sftpd.service
4828

被折叠的 条评论
为什么被折叠?



