首先创建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