腾讯云ubuntu20.04,免密登录,安装anaconda,并安装远程jupyter notebook,TensorFlow
免密登录
腾讯云服务器默认用户名为ubuntu,登录每次都要超过8位复杂密码,先做个免密。登录后直接输入:
ssh-keygen -t rsa
直接回车三联。
在当前家目录下会生成一个.ssh/目录。输入:
ls -a .ssh/
可以看到生成了一对密钥。
id_rsa id_rsa.pub
id_rsa是私钥,要传回客户机,重新打开一个客户端cmd命令行使用如下命令:
scp ubuntu@ip:/home/ubuntu/.ssh/id_rsa C:\Users\Administrator\.ssh
本文ip均为服务器IP,然后按要求输入用户密码直接下载下来。要确保是当前用户目录,我的就是administrator。
然后就是公钥输入到.ssh/下的authorized_keys文件,这时还没有这个文件,直接输入
cat .ssh/id_rsa.pub >> ./.ssh/authorized_keys
登录试试:
ssh ubuntu@ip
当然也可以在客户端修改C:\Windows\System32\drivers\etc\hosts文件,
新加一行ip (tab键)m
登录只要:
ssh ubuntu@m
登陆成功,无需密码。
这时就可以删除服务器上的私钥了。
rm -rf .ssh/id_rsa
谨慎使用rm呦。
下载anaconda
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
可以按需要从清华源下载
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
以上命令会在当前目录下载Anaconda3-5.3.1-Linux-x86_64.sh
运行安装:
bash Anaconda3-5.3.1-Linux-x86_64.sh
欢迎界面一直按住回车,直到要输入yes.回车。
出现:
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
回车,安装到当前目录。
Do you wish the installer to initialize Anaconda3
in your /home/ubuntu/.bashrc ? [yes|no]
加入环境变量,yes,回车。
Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
安装VScode,千万要选no。
安装完成。
输入:
conda --version
提示:
conda: command not found
重新加载环境变量:
source .bashrc
还不行可以手动加到环境变量。
下面升级到最新版conda(最好别升级,只是记录一下用法):
conda update conda
查看python版本:
python --version
Python 3.10.6或Python 3.7.0
一切求稳,重装系统每次都不一样,是哪种就不要改了,我是3.7.0。
创建个python环境命名为py37:
conda create --name py37 python=3.7
中间会暂停问是否继续,默认yes,回车就行。
完成后,进入环境py37:
conda activate py37
ubuntu@VM-4-6-ubuntu:~$ conda activate py37
(py37) ubuntu@VM-4-6-ubuntu:~$
出现上面变化,表示进入成功
退出输入:
conda deactivate
此时进入base环境,再输入上一命令退出base:
(py37) ubuntu@VM-4-6-ubuntu:~$ conda deactivate
(base) ubuntu@VM-4-6-ubuntu:~$ conda deactivate
ubuntu@VM-4-6-ubuntu:~$
安装TensorFlow
进入py37环境并输入:
conda install tensorflow
#(中间输个yes,回车就安装完成了)
创建个py文件验证是否安装成功:
vim 0001.py
内容:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))
运行:
python 0001.py
得到:
2022-10-10 20:54:36.456544: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-10-10 20:54:36.457382: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
2022-10-10 20:54:36.457913: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
b'Hello, TensorFlow!'
出来就行,主要是tf1和2不兼容。
安装jupyter notbook
conda install jupyter notebook
生成配置文件:
jupyter notebook --generate-config
会在当前目录下生成.jupyter/jupyter_notebook_config.py
修改配置文件:
vim ~/.jupyter/jupyter_notebook_config.py
把下面两行做以下修改:
121#c.NotebookApp.allow_remote_access =false
297#c.NotebookApp.ip = 'localhost'
401#c.NotebookApp.open_browser = True
#主意删除井号和空格
c.NotebookApp.allow_remote_access =True
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
我没改端口,默认8888.还要从腾讯云防火墙打开8888端口。
配置完成,开始运行:
jupyter notebook --allow-root
会发现给出一个url地址:http://(VM-4-6-ubuntu or 127.0.0.1):8888/?token=2c61cb82969cf9df480681f696bfdd4f32903183a3b4c45d
这时只要在自己浏览器中输入:http://m:8888/?token=2c61cb82969cf9df480681f696bfdd4f32903183a3b4c45d
因为我修改了hosts文件所以把括号换成m,没修改过直接用ip.
停止:Ctrl C
为了安全,长时间没有交互会自动断开连接,而jupyter停止运行,这时就要设置后台运行。使用以下命令:
nohup jupyter notebook > jupyter.log 2>&1 &
返回进程号:
16552
。
结束可以直接kill 16552
。
也可以
ps -ef|grep jupyter
发现:
(py37) ubuntu@VM-4-6-ubuntu:~$ ps -ef|grep jupyter
ubuntu 16552 16237 0 11:06 pts/0 00:00:00 /home/ubuntu/anaconda3/envs/py37/bin/python /home/ubuntu/anaconda3/envs/py37/bin/jupyter-notebook
ubuntu 16903 16237 0 11:08 pts/0 00:00:00 grep --color=auto jupyter
jupyter进程号16552,kill 16552
就行。
用一下试试:
完美,但是前面有一次导入tf时会找不到包,找了半天可能是什么内核没装,tf2兼容性不好,兼容性就弄不了了,重装系统可能更好。内核环境使用以下命令安装:
python -m ipykernel install --user --name py37 --display-name "Python (other-env)"
以上命令都要进入py37环境执行。