linux创建新用户,并且共享anaconda,并且修改conda中的config配置,使得包和环境安装到自己的文件夹下

完整思路

1、从官网下载anaconda3,并安装

参考连接:https://zhuanlan.zhihu.com/p/600780684
在这里插入图片描述

2、在root用户下,修改bashrc如下

在这里插入图片描述
        注意!!根据自己的路径进行修改!!我这里将anaconda3安装在了/root/anaconda3中

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!

# 存储conda初始化的配置命令
__conda_setup="$('/root/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"

# 检查上一条命令的执行结果,如果成功则执行配置命令
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
# 如果上一条命令执行失败,则尝试使用其他方式进行配置
else
    # 检查是否存在conda.sh文件,如果存在则执行该文件
    if [ -f "/root/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/root/anaconda3/etc/profile.d/conda.sh"
    # 如果conda.sh文件不存在,则将conda的安装路径添加到环境变量中
    else
        export PATH="/root/anaconda3/bin:$PATH"
    fi
fi

# 清除存储的conda配置命令变量
unset __conda_setup
# <<< conda initialize <<<

# 修改新用户的conda配置文件:  (使得各自用户的包安装到自己的文件夹内)
conda config --add envs_dirs ~/.conda/envs
conda config --add pkgs_dirs ~/.conda/pkgs

conda activate base

echo "(●'◡'●) 所有环境成功启动!(●'◡'●)"

3、一键式创建新用户

    在创建用户之前需要建立新的用户组,我这里名字叫做conda_group。这样创建的新用户就可以共享这个

# 添加conda_group用户组
root@xxxx:~# groupadd conda_group
# 将安装好的anaconda3文件夹的用户组修改为conda_group,并赋予权限
root@xxxx:~# chgrop -R conda_group /root/anancanda3 
# 不要忘记给上级目录/root赋予读和执行权限,不然其他目录下的用户无法访问root目录
root@xxxx:~# cd ..
root@xxxx:~# chmod 755 /root

在这里插入图片描述

#!/bin/bash (create_user.sh)

# 设置普通用户登录目录
login_dir="/home/user_home/"

unset username

# 获取用户名
read -p "请输入新用户的用户名: " username

# 创建用户, 并将其添加到 conda_group 组中
sudo adduser $username
sudo usermod -a -G conda_group $username

# 把~/.bashrc 复制到新用户的文件目录下
cp "/root/.bashrc" "$login_dir$username"
cp "/root/.profile" "$login_dir$username"

# 设置登录目录
sudo usermod --home "$login_dir$username" "$username"

# 修改目录所有者
sudo chown -R $username:$username "$login_dir$username"

# 设置用户密码
sudo passwd $username

echo "用户 $username 创建完成,并成功设置登录目录为 $login_dir$username,并将目录所有者修改为 $username"
# 启动上面的脚本
root@xxxx:~# ./create_user.sh

乱七八糟

一键式运行脚本

users=("[username]")

for i in ${users[*]}
do
   eval useradd $i #添加用户
   echo "xxx" | passwd --stdin $i #设置密码为xxx,--stdin后为用户名字

   eval usermod -s /bin/bash $i #修改用户登入后所使用的shell
   usermod -d /home/$i $i #修改用户登入后所使用的目录
   eval cp ~/.bashrc /home/$i/ #将关键配置文件复制到用户(注意,这个文件一般要重写)
   eval chown -R $i:$i /home/$i #设置文件所有者
done

linux整体操作相关命令

### 管理员添加新用户

首先使用root账号登录服务器,并
- 创建新用户,并将其添加到 conda_group 组中:  
`useradd -g conda_group [username]`  
修改新用户密码:  
`passwd  [username]`
- 把~/.bashrc 复制到新用户的文件目录下:  (这个文件记得重写,后面贴上了)
`cp ~/.bashrc /home/[username]/`


### 普通用户创建虚拟环境

新用户账号登录服务器,并
- 修改新用户的conda配置文件:  (使得各自用户的包安装到自己的文件夹内)
`conda config --add envs_dirs /home/[username]/.conda/envs`  
`conda config --add pkgs_dirs /home/[username]/.conda/pkgs`

如需在服务器上运行自己的Python项目,应首先创建一个虚拟环境,然后将项目部署到该虚拟环境中运行。创建虚拟环境:  
`conda create -n [env_name] python=x.x`  
其中,env_name 为环境名,python=x.x 用来指定该环境下的Python版本。例如,用户创建【Python3.6版本】的名为【xxx】的环境,则输入命令:  
`conda create -n xxx python=3.6`

注意事项:
- 服务器中已经独立安装 CUDA(11.4版本),支持GPU加速。
- 如需使用 PyTorch 或 TensorFlow ,请在项目所在的虚拟环境中下载安装其**GPU版本**。

相关配置文件

在这里插入图片描述

# .bashrc
# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

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

conda activate base
export  PATH=/usr/local/cuda/bin:$PATH
export  LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

    通过下面这个文件,使得用户登录时,自动进入base环境

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

修改config,使得子用户之间创建虚拟环境互不干扰

新用户账号登录服务器,并
- 修改新用户的conda配置文件:  
`conda config --add envs_dirs /home/[username]/.conda/envs`  
`conda config --add pkgs_dirs /home/[username]/.conda/pkgs`
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值