在腾讯云购买了一台轻量云服务器跑几个机器人用, 一篇基础配置的实践笔记
- 系统是
Debian 11
, 理论上Ubuntu
等衍生版本基本可以无修改地参考- 添加普通用户
- 安装Docker
- 安装Node.js
- 安装miniconda
- 安装Jupyter Notebook及后台启动
- Q&A
- 参考资料
先关机; 重置密码(设定root密码); 开机
添加用户
// 先切换到root
su root
<输入root密码, 也就是重置过的密码>
// 添加一个普通用户
sudo adduser <userName>
// 确定该用户的存在, 新建的用户应该在输出内容的最下面一行
cat /etc/passwd
// 将该用户加入sudoers
vi /etc/sudoers
<按下 i 键, 进入插入模式>
// 写入这个普通用户的权限
userName ALL=(ALL:ALL) ALL
// P.S.如果想在sudo的时候不输入密码就这样写
userName ALL=(ALL) NOPASSWD:ALL
// 保存退出
:wq
// 如果出现"readonly"之类的提示就↓
:wq!
//切换至普通用户
su <userName>
修改ssh配置
// 编辑ssh配置
sudo vi /etc/ssh/sshd_config
// 新加一个ssh端口
Port <portNumber>
Port 22
// 检查一下下列配置项
PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes
#MaxAuthTries <自己定>
#MaxSessions <自己定>
#PubketAuthentication yes
//重启ssh服务
sudo service ssh restart
此时已经可以使用密码登录了
btw, 使用其他端口登录的语法是ssh -p <PortNumber> <userName>@<IP>
如果想使用密钥登录就继续进行以下配置:
<在本地机器上>
// 创建公钥(打开一个终端/DOS/PS)
ssh-keygen
<在服务器上>
// 创建ssh认证
sudo vi /home/<UserName>/.ssh/authorized_keys
<把公钥复制进去, 就是id_rsa.pub里面的内容>
<wq保存退出>
冒号后面要输入的内容就是你想保存的地址, 如果默认就把前面括号里的抄一遍就行, 然后在那个目录下能看到的id_rsa.pub
就是生成的公钥, 用记事本打开复制就好
安装及使用防火墙ufw
(这个我后来并没有下载, 因为看到控制台有防火墙设置就没装, lol)
另外, 服务器ssh安全可参考👉服务器安全篇【ssh安全】
// 安装ufw
sudo apt install ufw
// 设定防火墙允许通过的端口
sudo ufw allow <port>
// 禁用22端口
sudo ufw deny 22
// 启动防火墙
sudo ufw enable
// 查看防火墙状态
sudo ufw status
修改shell
修改shell的原因是Debian 11 的默认shell是
Dash
而非Bash
, 二者的区别简单的说就是前者兼容性更差, 以笔者目前的水平还是换到Bash
图个省心
// 查看当前shell
ls -l `which sh`
// 将默认的Dash修改到修改到Bash
sudo dpkg-reconfigure dash
<在默认选项里选 no 就切换到Bash了>
创建项目文件夹
sudo mkdir Project
安装Docker
// 卸载旧版本的
sudo apt-get remove docker docker-engine docker.io containerd runc
// Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
//Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.
// 这里不知道是哪里的分段出问题了复制进去并不好使, 建议直接从官方指引里复制
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
// Test run
sudo docker run hello-world
安装Node.js
# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt-get install -y nodejs
安装miniconda
官方指南👉RPM and Debian Repositories for Miniconda
非官方指引👉conda的安装与使用(2021-04-27更新)
# Install our public GPG key to trusted store
curl https://repo.anaconda.com/pkgs/misc/gpgkeys/anaconda.asc | gpg --dearmor > conda.gpg
install -o root -g root -m 644 conda.gpg /usr/share/keyrings/conda-archive-keyring.gpg
# Check whether fingerprint is correct (will output an error message otherwise)
gpg --keyring /usr/share/keyrings/conda-archive-keyring.gpg --no-default-keyring --fingerprint 34161F5BF5EB1D4BFBBB8F0A8AEB4F8B29D82806
# Add our Debian repo
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/conda-archive-keyring.gpg] https://repo.anaconda.com/pkgs/misc/debrepo/conda stable main" > /etc/apt/sources.list.d/conda.list
**NB:** If you receive a Permission denied error when trying to run the above command (because `/etc/apt/sources.list.d/conda.list` is write protected), try using the following command instead:
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/conda-archive-keyring.gpg] https://repo.anaconda.com/pkgs/misc/debrepo/conda stable main" | sudo tee -a /etc/apt/sources.list.d/conda.list
# Install it!
apt update
apt install conda
#
source /opt/conda/etc/profile.d/conda.sh
conda -V
P.S. 报错没有source命令
的原因是Debian的默认Shell是Dash
而不是Bash
, 于是先执行下面的修改Shell之后再使用source才能完成安装
修改shell
// 查看当前shell
ls -l `which sh`
// 将默认的Dash修改到修改到Bash
sudo dpkg-reconfigure dash
<在默认选项里选 no 就切换到Bash了>
另外非特权用户每次建立连接以后都需要输入一遍source /opt/conda/etc/profile.d/conda.sh
之后才能正常使用, 鉴于我 总是记不住 不想记这个路径, 所以
- 可以把这个加入
bashrc
- 可以在自己的目录下面写一个简单的
conda.sh
#! /bin/bash
source /opt/conda/etc/profile.d/conda.sh
然后每次登录之后只需要. conda.sh
就可以使用了🤠
关于shell的"最速"实践, 可参考👉linux 的基本操作(编写shell 脚本)
安装Jupyter Notebook
// 查看当前虚拟环境
conda env list
// 使用miniconda创建一个虚拟环境
conda create -n <envName> python = <版本号>
// 激活虚拟环境
conda activate <envName>
// 下载安装Notebook
// 这里用pip下载也只是图个方便...
pip install jupyter notebook
// 生成配置文件
jupyter lab --generate-config
<记住这个文件生成的路径>
// 设置密码, 先启动一个Python终端
//(在终端里输入这个就能进入到命令行版的Python终端)
python3
>>> from notebook.auth import passwd; passwd()
<输入两次密码>
<把得到的输出复制到c.NotebookApp.password = '<这里>'>
// 打开配置文件, 在生成的时候的路径
sudo vi /.jupyter/jupyter_notebook_config.py
// 直接在前排添加以下内容
// (去掉这些项的注释的方法太慢而且太分散了, 不便于管理)
c.NotebookApp.allow_password_change = False
c.NotebookApp.allow_remote_access = True
c.NotebookApp.open_browser = False
c.NotebookApp.password = '<passwd()后输出的字符串>'
c.NotebookApp.password_required = True
c.NotebookApp.port = <portNumber>
c.NotebookApp.ip = '*'
// 前台启动命令
jupyter notebook
之后就可以在地址栏直接用<公网IP>:<portNumber>
正常访问了
后台继续保持运行(关闭ssh连接后也能继续保持运行)
nohup jupyter notebook
P.S. conda的其他命令
// 退出环境
conda deactivate
// 删除环境
conda remove -n <envName> --all
最后的最后
可能会发现此时的VS Code
在远程下还不能保存文件, 就是简单的权限问题, 给相应的文件夹下chmod
赋权即可, 如果需要可以使用-R
对目录下所有子目录都赋予同样的权限(必须是大写的)
sudo chmod -R 777 Project
(这句仅针对我自己的使用, 777是很有风险的, 务必谨慎使用)
关于
chmod
可参考👉Linux chmod命令
到这里已经足够我开始码了, 后面的过程 咕咕咕 随着进度更新!
Q&A
配置和运行过程中遇到的bug以及解决方案, 单独整理一篇
参考资料
单独整理一篇