Let’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 create 10 such users:
#!/bin/bash
for i in {1..10};
do
USER=test_t
USER+=$i
USER+=_1_1
echo $USER
GROUP=sftpusers
echo $GROUP
egrep "^$USER" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "WARNING: [$USER] already exists"
continue
fi
PASSWORD_=Password123!
PASSWORD_=$(perl -e 'print crypt($ARGV[0], "salty")' $PASSWORD_)
echo $PASSWORD_
adduser -p $PASSWORD_ $USER
usermod -g $GROUP $USER
usermod -s /bin/nologin $USER
chown root:$USER /home/$USER
chmod 755 /home/$USER
mkdir /home/$USER/writable
chown $USER:$GROUP /home/$USER/writable
chmod ug+rwX /home/$USER/writable
done;
When we execute, we will see the following output
# ./create-sftp-user.sh
test_t1_1_1
sftpusers
saMNhdbYqwtng
test_t2_1_1
sftpusers
saMNhdbYqwtng
test_t3_1_1
sftpusers
saMNhdbYqwtng
test_t4_1_1
sftpusers
saMNhdbYqwtng
test_t5_1_1
sftpusers
saMNhdbYqwtng
test_t6_1_1
sftpusers
saMNhdbYqwtng
test_t7_1_1
sftpusers
saMNhdbYqwtng
test_t8_1_1
sftpusers
saMNhdbYqwtng
test_t9_1_1
sftpusers
saMNhdbYqwtng
test_t10_1_1
sftpusers
saMNhdbYqwtng
We can confirm the users were created like so:
# cat /etc/passwd
...
test_t1_1_1:x:1002:1001::/home/test_t1_1_1:/bin/nologin
test_t2_1_1:x:1003:1001::/home/test_t2_1_1:/bin/nologin
test_t3_1_1:x:1004:1001::/home/test_t3_1_1:/bin/nologin
test_t4_1_1:x:1005:1001::/home/test_t4_1_1:/bin/nologin
test_t5_1_1:x:1006:1001::/home/test_t5_1_1:/bin/nologin
test_t6_1_1:x:1007:1001::/home/test_t6_1_1:/bin/nologin
test_t7_1_1:x:1008:1001::/home/test_t7_1_1:/bin/nologin
test_t8_1_1:x:1009:1001::/home/test_t8_1_1:/bin/nologin
test_t9_1_1:x:1010:1001::/home/test_t9_1_1:/bin/nologin
test_t10_1_1:x:1011:1001::/home/test_t10_1_1:/bin/nologin
Happy testing!
Resources:
- 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…%20More%20
- https://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
thanks, very interesting 🙂
Launch the financial Bot now to start earning. Telegram – @Cryptaxbot
Everyone can earn as much as he wants now. Telegram – @Cryptaxbot
One dollar is nothing, but it can grow into $100 here. Telegram – @Cryptaxbot
The best way for everyone who rushes for financial independence. Telegram – @Cryptaxbot
Start your online work using the financial Robot. Telegram – @Cryptaxbot
Let the financial Robot be your companion in the financial market. Telegram – @Cryptaxbot
The huge income without investments is available, now! Telegram – @Cryptaxbot
The additional income for everyone. Telegram – @Cryptaxbot