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

    1. cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/sftpd.service
    2. cp /etc/pam.d/sshd /etc/pam.d/sftpd
    3. cp /etc/ssh/sshd_config /etc/ssh/sftpd_config
    4. ln -sf /usr/sbin/service /usr/sbin/rcsftpd
    5. ln -sf /usr/sbin/sshd /usr/sbin/sftpd
    6. cp /etc/sysconfig/sshd /etc/sysconfig/sftp
    7. cp /var/run/sshd.pid /var/run/sftpd.pid
    8. cat > /etc/systemd/system/sftpd.service<<EOF
    9. [Unit]
    10. Description=OpenSSH server daemon
    11. #Documentation=man:sshd(8) man:sshd_config(5)
    12. Description=Sftp server daemon
    13. After=network.target sshd-keygen.service
    14. Wants=sshd-keygen.service
    15. [Service]
    16. Type=notify
    17. #EnvironmentFile=/etc/sysconfig/sshd
    18. #ExecStart=/usr/sbin/sshd -D $OPTIONS
    19. EnvironmentFile=-/etc/sysconfig/sftpd
    20. ExecStart=/usr/sbin/sftpd -f /etc/ssh/sftpd_config
    21. ExecReload=/bin/kill -HUP $MAINPID
    22. KillMode=process
    23. Restart=on-failure
    24. RestartSec=42s
    25. [Install]
    26. WantedBy=multi-user.target
    27. EOF
    28. [root@n9e ~]# grep ^[^#] /etc/pam.d/sftpd
    29. auth required pam_tally2.so deny=6 unlock_time=300
    30. auth required pam_sepermit.so
    31. auth substack password-auth
    32. auth include postlogin
    33. -auth optional pam_reauthorize.so prepare
    34. account required pam_nologin.so
    35. account include password-auth
    36. password include password-auth
    37. session required pam_selinux.so close
    38. session required pam_loginuid.so
    39. session required pam_selinux.so open env_params
    40. session required pam_namespace.so
    41. session optional pam_keyinit.so force revoke
    42. session include password-auth
    43. session include postlogin
    44. -session optional pam_reauthorize.so prepare
    45. [root@n9e ~]#
    46. [root@n9e home]# grep ^[^#] /etc/ssh/sftpd_config
    47. cat > /etc/ssh/sftpd_config<<EOF
    48. Port 22222
    49. HostKey /etc/ssh/ssh_host_rsa_key
    50. HostKey /etc/ssh/ssh_host_ecdsa_key
    51. HostKey /etc/ssh/ssh_host_ed25519_key
    52. SyslogFacility AUTHPRIV
    53. PermitRootLogin yes
    54. AuthorizedKeysFile .ssh/authorized_keys
    55. PasswordAuthentication yes
    56. ChallengeResponseAuthentication no
    57. GSSAPIAuthentication yes
    58. GSSAPICleanupCredentials no
    59. UsePAM yes
    60. X11Forwarding yes
    61. UseDNS no
    62. Banner /etc/issue.net
    63. AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    64. AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    65. AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    66. AcceptEnv XMODIFIERS
    67. PidFile /var/run/sftpd.pid
    68. Subsystem sftp internal-sftp
    69. Match Group sftp
    70. X11Forwarding no
    71. AllowTcpForwarding no
    72. ForceCommand internal-sftp
    73. ChrootDirectory /data/sftp/%u
    74. EOF
    75. [root@n9e ~]# cat /etc/sysconfig/sftp
    76. # Configuration file for the sshd service.
    77. # The server keys are automatically generated if they are missing.
    78. # To change the automatic creation uncomment and change the appropriate
    79. # line. Accepted key types are: DSA RSA ECDSA ED25519.
    80. # The default is "RSA ECDSA ED25519"
    81. # AUTOCREATE_SERVER_KEYS=""
    82. # AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519"
    83. # Do not change this option unless you have hardware random
    84. # generator and you REALLY know what you are doing
    85. SSH_USE_STRONG_RNG=0
    86. # SSH_USE_STRONG_RNG=1
    87. [root@n9e ~]#
    88. [root@n9e ~]# grep ^[^#] /etc/sysconfig/sftp
    89. SSH_USE_STRONG_RNG=0
    90. [root@n9e ~]#
    91. > /var/run/sftpd.pid
    92. systemctl daemon-reload
    93. systemctl enable sftpd.service
    94. systemctl restart sftpd.service
    95. 锁定家目录:
    96. groupadd sftp
    97. useradd -g sftp -s /bin/false mysftp
    98. passwd mysftp
    99. mkdir -p /data/sftp/mysftp
    100. usermod -d /data/sftp/mysftp mysftp
    101. chown root:sftp /data/sftp/mysftp
    102. chmod 755 /data/sftp/mysftp
    103. 读写目录:
    104. chmod 775 /data/sftp/mysftp-witer
    105. systemctl restart sftpd.service
    106. mkdir -p /home/mysftp/.ssh
    107. chown mysftp:sftp /home/zhangsan/.ssh
    108. chmod 700 /home/mysftp/.ssh
    109. cat xxx.pub >/home/mysftp/.ssh/authorized_keys
    110. chown mysftp:sftp /home/mysftp/.ssh/authorized_keys
    111. chmod 700 /home/mysftp/.ssh/authorized_keys
    112. 目录的权限设定有两个要点:
    113. 1、由ChrootDirectory指定的目录开始一直往上到系统根目录为止的目录拥有者都只能是root
    114. 2、由ChrootDirectory指定的目录开始一直往上到系统根目录为止都不可以具有群组写入权限
    115. [root@n9e ~]# ll /data/
    116. total 0
    117. drwxr-xr-x 5 root root 68 Oct 26 09:05 filebrowser
    118. drwxr-xr-x 3 root root 20 Dec 11 19:23 sftp
    119. [root@n9e ~]# cd /data/sftp/
    120. [root@n9e sftp]# pwd
    121. /data/sftp
    122. [root@n9e sftp]# ll
    123. total 0
    124. drwxr-xr-x 2 root sftp 6 Dec 11 19:23 mysftp
    125. [root@n9e sftp]#
    126. [root@n9e sftp]# cd mysftp/
    127. [root@n9e mysftp]# ls -l
    128. total 0
    129. [root@n9e mysftp]# touch test
    130. [root@n9e mysftp]# ll
    131. total 0
    132. -rw-r--r-- 1 root root 0 Dec 11 19:40 test
    133. [root@n9e mysftp]#

    image.png

    image.png

    image.png

    1. [chroot@riyimei ~]$ sftp -oPort=22222 -v mysftp@192.168.11.81
    2. OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
    3. debug1: Reading configuration data /etc/ssh/ssh_config
    4. debug1: /etc/ssh/ssh_config line 58: Applying options for *
    5. debug1: Connecting to 192.168.11.81 [192.168.11.81] port 22222.
    6. debug1: Connection established.
    7. debug1: SELinux support disabled
    8. debug1: key_load_public: No such file or directory
    9. debug1: identity file /home/chroot/.ssh/id_rsa type -1
    10. debug1: key_load_public: No such file or directory
    11. debug1: identity file /home/chroot/.ssh/id_rsa-cert type -1
    12. debug1: key_load_public: No such file or directory
    13. debug1: identity file /home/chroot/.ssh/id_dsa type -1
    14. debug1: key_load_public: No such file or directory
    15. debug1: identity file /home/chroot/.ssh/id_dsa-cert type -1
    16. debug1: key_load_public: No such file or directory
    17. debug1: identity file /home/chroot/.ssh/id_ecdsa type -1
    18. debug1: key_load_public: No such file or directory
    19. debug1: identity file /home/chroot/.ssh/id_ecdsa-cert type -1
    20. debug1: key_load_public: No such file or directory
    21. debug1: identity file /home/chroot/.ssh/id_ed25519 type -1
    22. debug1: key_load_public: No such file or directory
    23. debug1: identity file /home/chroot/.ssh/id_ed25519-cert type -1
    24. debug1: Enabling compatibility mode for protocol 2.0
    25. debug1: Local version string SSH-2.0-OpenSSH_7.4
    26. debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
    27. debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
    28. debug1: Authenticating to 192.168.11.81:22222 as 'mysftp'
    29. debug1: SSH2_MSG_KEXINIT sent
    30. debug1: SSH2_MSG_KEXINIT received
    31. debug1: kex: algorithm: curve25519-sha256
    32. debug1: kex: host key algorithm: ecdsa-sha2-nistp256
    33. debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    34. debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    35. debug1: kex: curve25519-sha256 need=64 dh_need=64
    36. debug1: kex: curve25519-sha256 need=64 dh_need=64
    37. debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    38. debug1: Server host key: ecdsa-sha2-nistp256 SHA256:xvABNXYzwRw/ORT/FVX8uFJ3p4hdc8PcZEdrPFmCGGQ
    39. debug1: checking without port identifier
    40. The authenticity of host '[192.168.11.81]:22222 ([192.168.11.81]:22222)' can't be established.
    41. ECDSA key fingerprint is SHA256:xvABNXYzwRw/ORT/FVX8uFJ3p4hdc8PcZEdrPFmCGGQ.
    42. ECDSA key fingerprint is MD5:8a:50:00:d0:ff:7d:5e:f7:90:80:06:76:02:18:7d:a1.
    43. Are you sure you want to continue connecting (yes/no)? yes
    44. Warning: Permanently added '[192.168.11.81]:22222' (ECDSA) to the list of known hosts.
    45. debug1: rekey after 134217728 blocks
    46. debug1: SSH2_MSG_NEWKEYS sent
    47. debug1: expecting SSH2_MSG_NEWKEYS
    48. debug1: SSH2_MSG_NEWKEYS received
    49. debug1: rekey after 134217728 blocks
    50. debug1: SSH2_MSG_EXT_INFO received
    51. debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
    52. debug1: SSH2_MSG_SERVICE_ACCEPT received
    53. Be sure you are authorized to access this system!
    54. debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
    55. debug1: Next authentication method: gssapi-keyex
    56. debug1: No valid Key exchange context
    57. debug1: Next authentication method: gssapi-with-mic
    58. debug1: Unspecified GSS failure. Minor code may provide more information
    59. No Kerberos credentials available (default cache: KEYRING:persistent:700)
    60. debug1: Unspecified GSS failure. Minor code may provide more information
    61. No Kerberos credentials available (default cache: KEYRING:persistent:700)
    62. debug1: Next authentication method: publickey
    63. debug1: Trying private key: /home/chroot/.ssh/id_rsa
    64. debug1: Trying private key: /home/chroot/.ssh/id_dsa
    65. debug1: Trying private key: /home/chroot/.ssh/id_ecdsa
    66. debug1: Trying private key: /home/chroot/.ssh/id_ed25519
    67. debug1: Next authentication method: password
    68. mysftp@192.168.11.81's password:
    69. debug1: Authentication succeeded (password).
    70. Authenticated to 192.168.11.81 ([192.168.11.81]:22222).
    71. debug1: channel 0: new [client-session]
    72. debug1: Requesting no-more-sessions@openssh.com
    73. debug1: Entering interactive session.
    74. debug1: pledge: network
    75. debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
    76. debug1: Sending environment.
    77. debug1: Sending env LANG = en_US.UTF-8
    78. debug1: Sending subsystem: sftp
    79. Connected to 192.168.11.81.
    80. sftp> ls
    81. test
    82. sftp> pwd
    83. Remote working directory: /
    84. sftp> lls
    85. sftp> lpwd
    86. Local working directory: /home/chroot
    87. sftp> cd /home
    88. Couldn't stat remote file: No such file or directory
    89. sftp> cd /data
    90. Couldn't stat remote file: No such file or directory
    91. sftp> ls
    92. test
    93. sftp> lpwd
    94. Local working directory: /home/chroot
    95. sftp> lls
    96. sftp>

    设置多用户权限和锁定家目录

    1. PidFile /var/run/sftpd.pid
    2. Subsystem sftp internal-sftp
    3. Match Group sftp
    4. X11Forwarding no
    5. AllowTcpForwarding no
    6. ForceCommand internal-sftp
    7. ChrootDirectory /data/sftp/%u
    8. Match User riyimei
    9. X11Forwarding no
    10. AllowTcpForwarding no
    11. ForceCommand internal-sftp
    12. ChrootDirectory /data/sftp/riyimei
    13. Match User liwm
    14. X11Forwarding no
    15. AllowTcpForwarding no
    16. ForceCommand internal-sftp
    17. ChrootDirectory /data/sftp/riyimei
    18. [root@n9e riyimei]#