SLBR/newuser.sh

28 lines
1.0 KiB
Bash
Raw Normal View History

2018-08-28 17:39:44 +00:00
#!/bin/sh
2018-09-14 17:39:19 +00:00
SLBRUSER=$1
2018-09-19 15:49:44 +00:00
PUBKEY=$2
test -z "$SLBRUSER" && echo "no user provided" && exit
2018-09-24 18:28:07 +00:00
test -z "$PUBKEY" && echo "no key provided" && exit
PUBKEY="$(cat "$PUBKEY")"
cut -d: -f1 /etc/passwd | grep -q "$SLBRUSER" && echo "user already exists" && exit
2018-08-28 17:39:44 +00:00
echo "Creating homedir..."
2018-09-14 17:39:19 +00:00
mkdir "/home/slbr/$SLBRUSER"
2018-09-19 20:43:25 +00:00
mkdir "/home/slbr/$SLBRUSER/.ssh"
echo "Copying key..."
echo "$PUBKEY" >> "/home/slbr/$SLBRUSER/.ssh/authorized_keys"
2018-09-24 18:28:07 +00:00
echo "$SLBRUSER" >> users.txt
2018-09-19 20:43:25 +00:00
echo "Creating container..."
2018-09-14 17:39:19 +00:00
echo "lxc exec $SLBRUSER bash" > "/home/slbr/$SLBRUSER/.profile"
chmod -w "/home/slbr/$SLBRUSER/.profile"
2018-08-28 17:39:44 +00:00
echo "Creating user..."
sudo useradd "$SLBRUSER" -d "/home/slbr/$SLBRUSER"
sudo usermod -a -G lxd "$SLBRUSER"
2018-09-19 20:43:25 +00:00
sudo chown -R "$SLBRUSER:$SLBRUSER" "/home/slbr/$SLBRUSER"
sudo chsh "$SLBRUSER" -s /home/slbr/login.sh
2018-09-19 20:43:25 +00:00
echo "Generating user keypair..."
sudo su "$SLBRUSER" -s /bin/sh -c "ssh-keygen -t rsa -N '' -f /home/slbr/$SLBRUSER/.ssh/id_rsa"
echo "Initializing container..."
sudo su "$SLBRUSER" -s /bin/sh -c "/home/slbr/lxcinit.sh"