https://www.cnblogs.com/huahuot/p/14010374.html
https://www.cnblogs.com/wooya/p/9392142.html
ssh和sftp通过openssh服务ftp加密访问但却和sah服务共用一个端口,安全基线是不符合,特别是通过公网与其他公司文件交互时 ssh的端口就完全被暴露在公网上,就算使用 /etc/hosts.alow限制IP地址访问也会涉及暴露ssh端口的风险,因此需要把sftp从ssh服务里分离出来,只暴露sftp的端口和锁定家目录 这样基于白名单和密钥验证方式部署ftp服务器才能保证安全
1 注释ssh配置文件里的sftp子服务
2 添加sftp服务和配置文件
3 锁定sftp用户家目录
该方式适用于为编译升级过openssh 系统部署 CentOS 7
cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/sftpd.service
cp /etc/pam.d/sshd /etc/pam.d/sftpd
cp /etc/ssh/sshd_config /etc/ssh/sftpd_config
ln -sf /usr/sbin/service /usr/sbin/rcsftpd
ln -sf /usr/sbin/sshd /usr/sbin/sftpd
cp /etc/sysconfig/sshd /etc/sysconfig/sftp
cp /var/run/sshd.pid /var/run/sftpd.pid
cat > /etc/systemd/system/sftpd.service<<EOF
[Unit]
Description=OpenSSH server daemon
#Documentation=man:sshd(8) man:sshd_config(5)
Description=Sftp server daemon
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
#EnvironmentFile=/etc/sysconfig/sshd
#ExecStart=/usr/sbin/sshd -D $OPTIONS
EnvironmentFile=-/etc/sysconfig/sftpd
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
[root@n9e ~]# grep ^[^#] /etc/pam.d/sftpd
auth required pam_tally2.so deny=6 unlock_time=300
auth required pam_sepermit.so
auth substack password-auth
auth include postlogin
-auth optional pam_reauthorize.so prepare
account required pam_nologin.so
account include password-auth
password include password-auth
session required pam_selinux.so close
session required pam_loginuid.so
session required pam_selinux.so open env_params
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include password-auth
session include postlogin
-session optional pam_reauthorize.so prepare
[root@n9e ~]#
[root@n9e home]# grep ^[^#] /etc/ssh/sftpd_config
cat > /etc/ssh/sftpd_config<<EOF
Port 22222
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UseDNS no
Banner /etc/issue.net
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
PidFile /var/run/sftpd.pid
Subsystem sftp internal-sftp
Match Group sftp
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory /data/sftp/%u
EOF
[root@n9e ~]# cat /etc/sysconfig/sftp
# Configuration file for the sshd service.
# The server keys are automatically generated if they are missing.
# To change the automatic creation uncomment and change the appropriate
# line. Accepted key types are: DSA RSA ECDSA ED25519.
# The default is "RSA ECDSA ED25519"
# AUTOCREATE_SERVER_KEYS=""
# AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519"
# Do not change this option unless you have hardware random
# generator and you REALLY know what you are doing
SSH_USE_STRONG_RNG=0
# SSH_USE_STRONG_RNG=1
[root@n9e ~]#
[root@n9e ~]# grep ^[^#] /etc/sysconfig/sftp
SSH_USE_STRONG_RNG=0
[root@n9e ~]#
> /var/run/sftpd.pid
systemctl daemon-reload
systemctl enable sftpd.service
systemctl restart sftpd.service
锁定家目录:
groupadd sftp
useradd -g sftp -s /bin/false mysftp
passwd mysftp
mkdir -p /data/sftp/mysftp
usermod -d /data/sftp/mysftp mysftp
chown root:sftp /data/sftp/mysftp
chmod 755 /data/sftp/mysftp
读写目录:
chmod 775 /data/sftp/mysftp-witer
systemctl restart sftpd.service
mkdir -p /home/mysftp/.ssh
chown mysftp:sftp /home/zhangsan/.ssh
chmod 700 /home/mysftp/.ssh
cat xxx.pub >/home/mysftp/.ssh/authorized_keys
chown mysftp:sftp /home/mysftp/.ssh/authorized_keys
chmod 700 /home/mysftp/.ssh/authorized_keys
目录的权限设定有两个要点:
1、由ChrootDirectory指定的目录开始一直往上到系统根目录为止的目录拥有者都只能是root
2、由ChrootDirectory指定的目录开始一直往上到系统根目录为止都不可以具有群组写入权限
[root@n9e ~]# ll /data/
total 0
drwxr-xr-x 5 root root 68 Oct 26 09:05 filebrowser
drwxr-xr-x 3 root root 20 Dec 11 19:23 sftp
[root@n9e ~]# cd /data/sftp/
[root@n9e sftp]# pwd
/data/sftp
[root@n9e sftp]# ll
total 0
drwxr-xr-x 2 root sftp 6 Dec 11 19:23 mysftp
[root@n9e sftp]#
[root@n9e sftp]# cd mysftp/
[root@n9e mysftp]# ls -l
total 0
[root@n9e mysftp]# touch test
[root@n9e mysftp]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 11 19:40 test
[root@n9e mysftp]#
[chroot@riyimei ~]$ sftp -oPort=22222 -v mysftp@192.168.11.81
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.11.81 [192.168.11.81] port 22222.
debug1: Connection established.
debug1: SELinux support disabled
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chroot/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.11.81:22222 as 'mysftp'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:xvABNXYzwRw/ORT/FVX8uFJ3p4hdc8PcZEdrPFmCGGQ
debug1: checking without port identifier
The authenticity of host '[192.168.11.81]:22222 ([192.168.11.81]:22222)' can't be established.
ECDSA key fingerprint is SHA256:xvABNXYzwRw/ORT/FVX8uFJ3p4hdc8PcZEdrPFmCGGQ.
ECDSA key fingerprint is MD5:8a:50:00:d0:ff:7d:5e:f7:90:80:06:76:02:18:7d:a1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.11.81]:22222' (ECDSA) to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
Be sure you are authorized to access this system!
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:700)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:700)
debug1: Next authentication method: publickey
debug1: Trying private key: /home/chroot/.ssh/id_rsa
debug1: Trying private key: /home/chroot/.ssh/id_dsa
debug1: Trying private key: /home/chroot/.ssh/id_ecdsa
debug1: Trying private key: /home/chroot/.ssh/id_ed25519
debug1: Next authentication method: password
mysftp@192.168.11.81's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.11.81 ([192.168.11.81]:22222).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending subsystem: sftp
Connected to 192.168.11.81.
sftp> ls
test
sftp> pwd
Remote working directory: /
sftp> lls
sftp> lpwd
Local working directory: /home/chroot
sftp> cd /home
Couldn't stat remote file: No such file or directory
sftp> cd /data
Couldn't stat remote file: No such file or directory
sftp> ls
test
sftp> lpwd
Local working directory: /home/chroot
sftp> lls
sftp>
设置多用户权限和锁定家目录
PidFile /var/run/sftpd.pid
Subsystem sftp internal-sftp
Match Group sftp
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory /data/sftp/%u
Match User riyimei
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory /data/sftp/riyimei
Match User liwm
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory /data/sftp/riyimei
[root@n9e riyimei]#