Skip to main content

KT3 Upload website

Maak lijst met kandidaten in Excel, met alle studenten nummers en verzin wachtwoorden.

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
else
    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
    useradd -m -s /bin/bash -p $pass $username
    [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"

    # set home dir (www)
    usermod -d /var/www/softwaredeveloper/$username $username
    # set user in sftp group (which restricts access)
    usermod -g sftp $username
fi

SFTP restriction in /etc/ssh/sshd_config (aan eind toevoegen)

Match Group sftp
ForceCommand internal-sftp
ChrootDirectory %h
AllowTCPForwarding no
PermitTunnel no
X11Forwarding no

SQL, create uer u<nummer> en maak db u<nummer> en geef user alle rechten.

CREATE USER `u2080050`@`%` IDENTIFIED BY 'KLewdertje-501';
ALTER USER  `u2080050`@`%` REQUIRE NONE;
CREATE DATABASE IF NOT EXISTS `u2080050`;
GRANT ALL PRIVILEGES ON `u2080050`.* TO `u2080050`@`%`;

Bash script om dit te maken:

username=$1
password=$2

echo ""
echo  "CREATE USER \`$username\`@\`%\` IDENTIFIED BY '$password';"
echo  "ALTER USER  \`$username\`@\`%\` REQUIRE NONE;";
echo  "CREATE DATABASE IF NOT EXISTS \`$username\`;";
echo  "GRANT ALL PRIVILEGES ON \`$username\`.* TO \`$username\`@\`%\`;"

Script add users

#!/bin/bash
# Script to add a user to Linux system
# Params <user> <passw> or <file>

DB_FILE=createDBUsers.sql

addThisUser() {
if [ $(id -u) -eq 0 ]; then

  echo "" >> $DB_FILE

  if [ "$1" == "" ]; then
    read -p "Enter username : " username
  else
    username=$1
  fi
  if [ "$2" == "" ]; then
    read -s -p "Enter password : " password
  else
    password=$2
  fi

  egrep "^$username" /etc/passwd >/dev/null
  if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
  else
    U_HOME=/var/www/softwaredeveloper/$username
    U_GROUP=sftp
    U_SHELL=/bin/bash

    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

    useradd -d $U_HOME -g $U_GROUP -m -s $U_SHELL -p $pass $username
    [ $? -eq 0 ] && echo "User $username has been added to system!" || echo "Failed to add a user!"

    chown $username $U_HOME
    echo "<?php echo \"welkom $username\"; ?>" > $U_HOME/index.php
    chown $username $U_HOME/index.php

    echo "" >> $DB_FILE
    echo  "CREATE USER \`$username\`@\`%\` IDENTIFIED BY '$password';" >> $DB_FILE
    echo  "ALTER USER  \`$username\`@\`%\` REQUIRE NONE;" >> $DB_FILE
    echo  "CREATE DATABASE IF NOT EXISTS \`$username\`;" >> $DB_FILE
    echo  "GRANT ALL PRIVILEGES ON \`$username\`.* TO \`$username\`@\`%\`;" >> $DB_FILE

  fi
else
  echo "Only root may add a user to the system"
  exit 2
fi
}

# is parameter is file then read input from file <user> Mpassword>
# else read two command line params
if test -f "$1"; then
  while IFS= read -r line
  do
    addThisUser $line
  done < $1
else
  addThisUser $1 $2
fi

script del users

#!/bin/bash
# remove user, params <user> or <file>

removeThisUser() {
if [ $(id -u) -ne 0 ]; then
  echo "Only root may add a user to the system"
  exit 2
fi

if [ "$1" == "" ]; then
  read -p "Enter username : " username
else
  username=$1
fi

if [ $? -eq 0 ]; then
  userdel -r $username
  getent group $username || groupadd $username


  U_HOME=/var/www/softwaredeveloper/$username

  echo "Remove $U_HOME"

fi
}


# is parameter is file then read input from file <user>
# else read  command line params
if test -f "$1"; then
    while IFS= read -r line
  do
    removeThisUser $line
  done < $1
else
  removeThisUser $1 $2
fi