{"id":19571,"date":"2017-05-24T06:15:22","date_gmt":"2017-05-23T22:15:22","guid":{"rendered":"https:\/\/2blog.ilc.edu.tw\/25793\/2017\/05\/24\/ssh-server-sftp-only-chroot\/"},"modified":"2021-03-18T17:25:02","modified_gmt":"2021-03-18T09:25:02","slug":"ssh-server-sftp-only-chroot","status":"publish","type":"post","link":"https:\/\/2blog.ilc.edu.tw\/25793\/2017\/05\/24\/ssh-server-sftp-only-chroot\/","title":{"rendered":"SSH Server &#8211; SFTP only + Chroot"},"content":{"rendered":"<p>\u53c3\u8003\u7db2\u7ad9\uff1a<br \/><a href=\"https:\/\/www.server-world.info\/en\/note?os=CentOS_7&amp;p=ssh&amp;f=5\" target=\"_blank\" rel=\"noopener noreferrer\">CentOS 7 : SSH Server : SFTP only + Chroot : Server World<\/a><\/p>\n<p>\u4ee5\u524d\u7684\u4f5c\u6cd5\u90fd\u662f\u4e0d\u958b\u653e\u4e00\u822c\u4f7f\u7528\u8005\u4f7f\u7528 ssh\uff0c\u8b93\u4f7f\u7528\u8005\u4f7f\u7528 ftp \u4f86\u50b3\u8f38\u6a94\u6848\uff0c\u4e0d\u904e FTP Server \u7b97\u662f\u6bd4\u8f03\u4e0d\u5b89\u5168\u7684\u901a\u8a0a\u5354\u5b9a\u800c\u4e14\u9084\u8981\u5b89\u88dd\u5efa\u7f6e FTP Server\uff0c\u6240\u4ee5\u6539\u7528 SFTP\uff0c\u56e0\u70ba\u5e33\u865f\u5bc6\u78bc\u53ca\u50b3\u8f38\u8cc7\u6599\u90fd\u6709\u7d93\u904e\u52a0\u5bc6\uff0c\u6703\u6bd4\u8f03\u5b89\u5168\u3002<br \/>\u4ee5\u524d\u7684\u4f5c\u6cd5\uff1a<br \/># <span style=\"color: #ff0000\">useradd test -d \/home\/test -m -s \/bin\/nologin<\/span><br \/>\u6539\u7528 SFTP \u7684\u4f5c\u6cd5\uff1a<br \/>1. \u5efa\u7acb sftp \u7fa4\u7d44<br \/># <span style=\"color: #ff0000\">groupadd sftp_users<\/span><br \/>2. \u65b0\u589e\u4f7f\u7528\u8005 <br \/># <span style=\"color: #ff0000\">useradd test -G sftp_users -d \/home\/test -m<\/span><br \/>\u5982\u679c\u4f7f\u7528\u8005\u5df2\u7d93\u5efa\u7acb\u5b8c\u6210<br \/># <span style=\"color: #ff0000\">usermod -G sftp_users test<\/span><br \/>3. \u8a2d\u5b9a\u5e33\u865f\u5bc6\u78bc<br \/># <span style=\"color: #ff0000\">passwd test<\/span><br \/><span style=\"color: #0000ff\">Changing password for user test.<\/span><br \/><span style=\"color: #0000ff\">New password:<\/span><br \/><span style=\"color: #0000ff\">Retype new password:<\/span><br \/><span style=\"color: #0000ff\">passwd: all authentication tokens updated successfully.<\/span><br \/>[@more@]4. \u4fee\u6539 SSH Server \u8a2d\u5b9a<br \/># <span style=\"color: #ff0000\">cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.$(date +%F)<\/span><br \/># <span style=\"color: #ff0000\">vim \/etc\/ssh\/sshd_config<\/span><br \/><span style=\"color: #0000ff\">#Subsystem\u00a0\u00a0\u00a0\u00a0\u00a0 sftp\u00a0\u00a0\u00a0 \/usr\/libexec\/openssh\/sftp-server<\/span><br \/><span style=\"color: #0000ff\">Subsystem\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sftp\u00a0\u00a0\u00a0 internal-sftp<\/span><\/p>\n<p><span style=\"color: #0000ff\">Match Group sftp_users<\/span><br \/><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 X11Forwarding no<\/span><br \/><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AllowTcpForwarding no<\/span><br \/><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ChrootDirectory \/home<\/span><br \/><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ForceCommand internal-sftp<\/span><\/p>\n<p>5. \u91cd\u65b0\u555f\u52d5 SSH Server<br \/># <span style=\"color: #ff0000\">systemctl restart sshd.service<\/span><\/p>\n<p>6. \u9032\u884c\u6e2c\u8a66<br \/># <span style=\"color: #ff0000\">ssh test@192.168.1.17<\/span><br \/><span style=\"color: #0000ff\">test@192.168.1.17&#8217;s password:<\/span><br \/><span style=\"color: #0000ff\">Could not chdir to home directory \/home\/test: No such file or directory<\/span><br \/><span style=\"color: #0000ff\">This service allows sftp connections only.<\/span><br \/><span style=\"color: #0000ff\">Connection to 192.168.1.17 closed.<\/span><\/p>\n<p># <span style=\"color: #ff0000\">sftp test@192.168.1.17<\/span><br \/><span style=\"color: #0000ff\">Connecting to 192.168.1.17&#8230;<\/span><br \/><span style=\"color: #0000ff\">test@192.168.1.17&#8217;s password:<\/span><br \/><span style=\"color: #0000ff\">sftp&gt; ls -l<\/span><br \/><span style=\"color: #0000ff\">drwx&#8211;x&#8211;x\u00a0\u00a0\u00a0 2 1000\u00a0\u00a0\u00a0\u00a0 1001\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4096 Mar 11 02:05 test<\/span><br \/><span style=\"color: #0000ff\">sftp&gt;<\/span> <span style=\"color: #ff0000\">bye<\/span><\/p>\n<p>\u4e00\u822c\u4f7f\u7528\u8005\u53ef\u4ee5\u4f7f\u7528 <a href=\"https:\/\/filezilla-project.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">FileZilla<\/a> \u6216\u662f <a href=\"https:\/\/winscp.net\/eng\/docs\/lang:cht\" target=\"_blank\" rel=\"noopener noreferrer\">WinSCP<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u7db2\u7ad9\uff1aCentOS 7 : SSH Server : SFTP only + Chroot : Serve &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/2blog.ilc.edu.tw\/25793\/2017\/05\/24\/ssh-server-sftp-only-chroot\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008SSH Server &#8211; SFTP only + Chroot\u3009<\/span><\/a><\/p>\n","protected":false},"author":55,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,139],"tags":[],"class_list":["post-19571","post","type-post","status-publish","format-standard","hentry","category-c-78462","category-c-78484"],"meta_key":{"_pingme":["1"],"_encloseme":["1"],"lt_post_id":["688382"]},"_links":{"self":[{"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/posts\/19571"}],"collection":[{"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/comments?post=19571"}],"version-history":[{"count":1,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/posts\/19571\/revisions"}],"predecessor-version":[{"id":21079,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/posts\/19571\/revisions\/21079"}],"wp:attachment":[{"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/media?parent=19571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/categories?post=19571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2blog.ilc.edu.tw\/25793\/wp-json\/wp\/v2\/tags?post=19571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}