Debian/Ubuntu云服务器配置最速实践笔记[1]

在腾讯云购买了一台轻量云服务器跑几个机器人用, 一篇基础配置的实践笔记

先关机; 重置密码(设定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保存退出>

ssh_key
冒号后面要输入的内容就是你想保存的地址, 如果默认就把前面括号里的抄一遍就行, 然后在那个目录下能看到的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

官方地址👉Install Docker Engine on Debian

// 卸载旧版本的
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

官方指导(GitHub README)👉NodeSource Node.js Binary Distributions

# 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之后才能正常使用, 鉴于我 总是记不住 不想记这个路径, 所以

  1. 可以把这个加入bashrc
  2. 可以在自己的目录下面写一个简单的conda.sh
#! /bin/bash
source /opt/conda/etc/profile.d/conda.sh

然后每次登录之后只需要. conda.sh就可以使用了🤠
conda.sh
关于shell的"最速"实践, 可参考👉linux 的基本操作(编写shell 脚本)

安装Jupyter Notebook

非官方配置指南👉云服务centos搭建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以及解决方案, 单独整理一篇

参考资料

单独整理一篇

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值