自动化创建用户

38 篇文章 1 订阅

首先创建shell脚本 create_sharefile.sh
如下是shell脚本的内容,可以在超级管理员账户创建
./create*.sh 名字 学位 入学时间
传入了三个参数 名字、学位和入学时间
$1,$2,$3
该脚本包含如下部分

  • 创建用户和默认密码
  • 指定用户的家目录
  • 为每个用户的.bashrc 中添加创建好的python环境
  • 将家目录下的myproject文件作为软连接指向以前创建好的目录
  • 改修文件的权限和组
#./create*.sh username degree year
#./create*.sh lus phd 2020 


USER_COUNT=`cat /etc/passwd | grep ^$1: -c`
USER_NAME=$1
Degree=$2
YEARS=$3
if [ $USER_COUNT -ne 1 ]
then
    useradd -g public  -s /bin/bash -d /data/Users/${USER_NAME} -m $USER_NAME
    echo $USER_NAME:${USER_NAME}207 | chpasswd
    echo "user $USER_NAME added!"
else
    echo 'user exits'
fi


# setting of  conda

echo '''



export CUDA_VISIBLE_DEVICES=0


# <<< conda init <<<

chgrp public /data/students/$Degree/$YEARS/$USER_NAME -R
chgrp public /data/Users/$USER_NAME/myProject -R

# rwx
chmod -R u=rwx,g=r,o=r /data/students/$Degree/$YEARS/$USER_NAME
chmod -R u=rwx,g=r,o=r /data/Users/$USER_NAME/myProject
export CUDA_VISIBLE_DEVICES=0 

# <<< conda init <<<

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/data/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/data/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/data/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/data/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
''' >> /data/Users/$USER_NAME/.bashrc

#link data to myproject
mkdir -p /data/students/$Degree/$YEARS/$USER_NAME
ln -s /data/students/$Degree/$YEARS/$USER_NAME /data/Users/$USER_NAME/myProject

#modify users
chown -R $USER_NAME /data/students/$Degree/$YEARS/$USER_NAME
chown -R $USER_NAME /data/Users/$USER_NAME/myProject

#modify Group

chgrp public /data/students/$Degree/$YEARS/$USER_NAME -R
chgrp public /data/Users/$USER_NAME/myProject -R

# rwx
chmod -R u=rwx,g=r,o=r /data/students/$Degree/$YEARS/$USER_NAME
chmod -R u=rwx,g=r,o=r /data/Users/$USER_NAME/myProject       

第二步、为每个用户创建共享文件

#./create*.sh username degree year
#./create*.sh lus phd 2020 


USER_NAME=$1
#Degree=$2
#YEARS=$3

#link data to share_file
sudo ln -s /data/students/Share_files /data/Users/$USER_NAME/share_files

#modify users
sudo chown -R public /data/students/Share_files
sudo chown $USER_NAME /data/Users/$USER_NAME/share_files

#modify Group

sudo chgrp public /data/students/Share_files -R
sudo chgrp public /data/Users/$USER_NAME/share_files -R

# rwx
sudo chmod -R u=rwx,g=rwx,o=--- /data/students/Share_files
sudo chmod u=rwx,g=rx,o=--- /data/Users/$USER_NAME/share_files
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值