自动添加Linux登录账户,并授予sudo权限

#!/bin/bash
USER=test
PASS=$USER
GROUP=root
HOME=/data/home/$USER
# if user not exist
if [[ $(cat /etc/passwd | grep -E '^$USER:' -c) == "0" ]]; then
  # use weak password
  sed 's/^password    requisite.*/password    requisite     pam_cracklib.so try_first_pass retry=3   type=/g' -i /etc/pam.d/system-auth
  # add user
  groupadd $GROUP 2>/dev/null
  mkdir -p /data/home && useradd $USER -d $HOME -g $GROUP
  # add sudo privilege
  sed '/$USER ALL=(ALL) NOPASSWD:ALL/d'  -i /etc/sudoers
  echo "$USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  # set password
  passwd $USER << EOM
$PASS
$PASS
EOM
fi
posted @ 2016-07-11 14:46  Florian  阅读(1175)  评论(0编辑  收藏  举报