{"id":650,"date":"2021-07-02T15:13:37","date_gmt":"2021-07-02T15:13:37","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=650"},"modified":"2021-07-02T15:13:39","modified_gmt":"2021-07-02T15:13:39","slug":"create-multiple-sftp-only-users-on-linux-via-script","status":"publish","type":"post","link":"http:\/\/kevinmichaelcoy.com\/blog\/2021\/07\/02\/create-multiple-sftp-only-users-on-linux-via-script\/","title":{"rendered":"Create Multiple SFTP-Only Users On Linux Via Script"},"content":{"rendered":"\n<p>Let&#8217;s say you need a handful of users for testing out multiple SFTP accounts.  Additionally, you do not want the users to be able to login via SSH and you want everything else about the users to be identical (ie, password, content of home directory, etc).<\/p>\n\n\n\n<p>The following script will create 10 such users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"> \n #!\/bin\/bash\n \n for i in {1..10};\n do\n \u00a0 \u00a0 USER=test_t\n \u00a0 \u00a0 USER+=$i\n \u00a0 \u00a0 USER+=_1_1\n \u00a0 \u00a0 echo $USER\n \u00a0 \u00a0 GROUP=sftpusers\n \u00a0 \u00a0 echo $GROUP\n\n \u00a0 \u00a0 egrep \"^$USER\" \/etc\/passwd >\/dev\/null\n \u00a0 \u00a0 if [ $? -eq 0 ]; then\n echo \"WARNING: [$USER] already exists\"\n \u00a0 \u00a0 \u00a0 \u00a0 continue\n \u00a0 \u00a0 fi\u00a0\n \n \u00a0 \u00a0 PASSWORD_=Password123!\n \u00a0 \u00a0 PASSWORD_=$(perl -e 'print crypt($ARGV[0], \"salty\")' $PASSWORD_)\n \u00a0 \u00a0 echo $PASSWORD_\n\n \u00a0 \u00a0 adduser -p $PASSWORD_ $USER\n \u00a0 \u00a0 usermod -g $GROUP $USER\n \u00a0 \u00a0 usermod -s \/bin\/nologin $USER\n \u00a0 \u00a0 chown root:$USER \/home\/$USER\n \u00a0 \u00a0 chmod 755 \/home\/$USER\n \u00a0 \u00a0 mkdir \/home\/$USER\/writable\n \u00a0 \u00a0 chown $USER:$GROUP \/home\/$USER\/writable\n \u00a0 \u00a0 chmod ug+rwX \/home\/$USER\/writable\n done; <\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>When we execute, we will see the following output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"> # .\/create-sftp-user.sh\u00a0\n test_t1_1_1\n sftpusers\n saMNhdbYqwtng\n test_t2_1_1\n sftpusers\n saMNhdbYqwtng\n test_t3_1_1\n sftpusers\n saMNhdbYqwtng\n test_t4_1_1\n sftpusers\n saMNhdbYqwtng\n test_t5_1_1\n sftpusers\n saMNhdbYqwtng\n test_t6_1_1\n sftpusers\n saMNhdbYqwtng\n test_t7_1_1\n sftpusers\n saMNhdbYqwtng\n test_t8_1_1\n sftpusers\n saMNhdbYqwtng\n test_t9_1_1\n sftpusers\n saMNhdbYqwtng\n test_t10_1_1\n sftpusers\n saMNhdbYqwtng <\/code><\/pre>\n\n\n\n<p>We can confirm the users were created like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># cat \/etc\/passwd \n...\n test_t1_1_1:x:1002:1001::\/home\/test_t1_1_1:\/bin\/nologin\n test_t2_1_1:x:1003:1001::\/home\/test_t2_1_1:\/bin\/nologin\n test_t3_1_1:x:1004:1001::\/home\/test_t3_1_1:\/bin\/nologin\n test_t4_1_1:x:1005:1001::\/home\/test_t4_1_1:\/bin\/nologin\n test_t5_1_1:x:1006:1001::\/home\/test_t5_1_1:\/bin\/nologin\n test_t6_1_1:x:1007:1001::\/home\/test_t6_1_1:\/bin\/nologin\n test_t7_1_1:x:1008:1001::\/home\/test_t7_1_1:\/bin\/nologin\n test_t8_1_1:x:1009:1001::\/home\/test_t8_1_1:\/bin\/nologin\n test_t9_1_1:x:1010:1001::\/home\/test_t9_1_1:\/bin\/nologin\n test_t10_1_1:x:1011:1001::\/home\/test_t10_1_1:\/bin\/nologin <\/code><\/pre>\n\n\n\n<p>Happy testing!<\/p>\n\n\n\n<p>Resources:<\/p>\n\n\n\n<ul><li>https:\/\/www.developerfiles.com\/how-to-create-sftp-access-to-a-single-folder-on-linux\/#:~:text=%20How%20to%20create%20SFTP%20access%20to%20a,on%20%E2%80%9C%20ChrootDirectory%20%E2%80%9D%20will%20be\u2026%20More%20<\/li><li>https:\/\/www.cyberciti.biz\/tips\/howto-write-shell-script-to-add-user.html<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you need a handful of users for testing out multiple SFTP accounts. Additionally, you do not want the users to be able to login via SSH and you want everything else about the users to be identical (ie, password, content of home directory, etc). The following script will&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/kevinmichaelcoy.com\/blog\/2021\/07\/02\/create-multiple-sftp-only-users-on-linux-via-script\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[433,435,432,434],"_links":{"self":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/650"}],"collection":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":1,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}