Linux系统管理初步·科学计算环境搭建(待续)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tony_tian_base/article/details/84075041

Linux系统管理初步·科学计算环境搭建(待续)

由于科研需要,负责管理一台服务器虚拟环境(Ubuntu),主要是用户权限管理、系统环境配置和SSH搭建。由于是第一次系统地接触Linux环境,因此许多东西都是从头开始摸索,踩了很多坑才勉力实现了初步的系统搭建和环境维护。

首先需要说明的是,我使用的是macOS环境,版本为macOS Mojave 10.14,服务器环境为Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-38-generic x86_64)。主要需要配置的是Python环境和FTP传输管理。服务器环境配置期间分配了一个公网IP以部署系统环境,部署完成之后将回归内网环境,届时将无法连接外部网络。

系统初次启动

远程登录服务器使用SSH协议完成,连接方式为在终端输入ssh [email protected],由于使用的服务器是服务器管理员分配的一个虚拟服务器空间,预先根据需要设置了根目录名和管理员账户。

系统初次启动,需要配置一下文件夹结构,由于本地文件结构初始化为/home/admin/路径,因此需要根据需要创建新的文件夹结构。

ls -ahl
mkdir python/
mkdir vftp/

初次使用Ubuntu环境,可能需要使用自带的工具下载一些环境包,这时可以使用apt-get完成,但在国内的网络环境中,从许多官方源下载数据文件速度很慢,因此推荐首先配置Ubuntu下载源(https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ )。推荐使用清华大学的开源镜像站TUNA来替换各种官方源。

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

在替换完更新源之后,运行sudo apt-get update更新一下。更新完后,即可安装一些必要的软件包,如npm,yum等。

本文主要配置的系统环境为Python环境,由于我对Anaconda使用较多,因此选择使用Anaconda搭建Python环境,并使用conda来管理不同Python环境。

Anaconda的安装

Anaconda的源文件地址为 https://repo.continuum.io/archive/ ,里面有各种版本的Anaconda,按喜好自取其链接即可,如 https://repo.continuum.io/archive/Anaconda3-5.3.0-Linux-x86_64.sh

在Linux上下载源文件:

扫描二维码关注公众号,回复: 4152062 查看本文章

wget https://repo.continuum.io/archive/Anaconda3-5.3.0-Linux-x86_64.sh

验证文件完整性:

sha256sum Anaconda3-5.3.0-Linux-x86_64.sh

验证完毕后,安装:

bash Anaconda3-5.3.0-Linux-x86_64.sh

安装完毕后更新路径以使用conda命令:

source ./.bashrc

在进行下面的步骤前,应先设置好各种更新工具的更新源,即将原始源替换为TUNA源,如condapipnpm等。替换方式类似于apt-get的更新,可在 https://mirrors.tuna.tsinghua.edu.cn/help/ 自行查找。

Anaconda 环境管理

# 新建tensorflow环境
conda create -n tensorflow pip python=3.6
source acticate tensorflow
pip install --ignore-installed --upgrade https://download.tensorflow.google.cn/linux/cpu/tensorflow-1.8.0-cp36-cp36m-linux_x86_64.whl
pip install tensorboard
pip install torchvision
pip install torch
# 将tensorflow环境添加至jupyter notebook环境菜单
conda install ipykernel
python -m ipykernel install --user --name tensorflow --display-name "tensorflow"

Jupyter Notebook & JupyterLab

Jupyter notebook 配置

生成配置文件:

jupyter notebook --generate-config

默认会放在.jupyter/jupyter_notebook_config.py路径,为后续更改配置做准备,先生成密钥:

ipython 
from notebook.auth import passwd 
passwd(password_u_desire)
#记录下输出的密文 形式为 sha:ce...

修改配置文件:

sudo vim .jupyter/jupyter_notebook_config.py

直接在文件末尾添加:

c.NotebookApp.ip='*' # *号意为任意ip均可连接
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False  # 打开时不使用浏览器界面
c.NotebookApp.port = 9999 #随便指定一个端口,也可以不加

配置完毕后,后台启动jupyter notebook:

nohup jupyter notebook --config .jupyter/jupyter_notebook_config.py > sys/log/jupyter.log &

nohup命令是后台运行进程,将日志文件存放在指定的路径,在此指定为:sys/log/jupyter.log,即不影响命令行进行其他操作,随时都可以使用cat sys/log/jupyterhub.log查看日志文件。

使用nohup命令时会在命令行输出进程编号如20231,在需要关闭后台进程时,使用kill 20231即可,当然也可以不用记忆进程编号,使用ps -ef | grep jupyter命令即可查看所有jupyter相关的后台进程。

启动后,可以在任意客户机浏览器上访问http://ip_address:port访问,输入密码验证成功后即可使用jupyter notebook服务。

但是,使用这种形式,在安全上和效率上都有欠缺之处,其安全问题暂时没有完美的解决方案(离线的Jupyterhub暂未配置成功,配置成功后不失为一种不错的解决方案),效率问题反映为多个用户使用同一端口登录时,会互相挤占运算资源,极端情况下甚至会干扰正常使用(如两个用户都需要使用jupyterlab时)。

对效率问题,可以使用一种替代性方法,即在后台运行多个jupyter notebook进程,分别对应不同的端口,不同用户访问不同端口即可。但此情形仍然不支持多用户同时使用jupyterlab

Jupyter notebook 多用户情形的配置

具体实现为:

cp .jupyter/jupyter_notebook_config.py  .jupyter/jupyter_notebook_config_usr1.py
sudo vi .jupyter/jupyter_notebook_config_usr1.py 
# 将端口修改为未使用过的端口 如 6666 7777 等

#启动后台进程
nohup jupyter notebook --config .jupyter/jupyter_notebook_config_usr1.py > sys/log/jupyter_usr1.log &
cat sys/log/jupyter_usr1.log

Jupyter notebook 多用户的终极解决方案:Jupyterhub

出于某些原因,暂未完成Jupyterhub的配置,主要影响因素是目前广为使用的解决方案需要配合Github或Gitlab验证使用,而本文的Linux环境未来将运行在内网上,无法使用此类验证方式。目前仍在研究内网Jupyterhub的配置方案。

用户文件传输的选择:SSH

出于某些原因,SSH比一般的FTP更加安全,也更好配置,因此使用SSH作为服务器文件传输的主要工具。

一般,SSH连接需要Linux普通用户权限:

# 设定共享文件夹权限
chmod -R 774 /home/lhd/vftp/
# 添加用户
useradd -g stu -d /home/lhd/vftp/ usr1
# 为usr1设定登录密码
passwd usr1

上述操作完成后即可使用usr1进行SSH连接。

我最终选择了Cyberduck作为SSH传输工具,主要原因是:图形化界面、操作简单。

猜你喜欢

转载自blog.csdn.net/tony_tian_base/article/details/84075041