Ubuntu 20.04 基础环境搭建

手头有台腾讯云学生机一直闲置,最近准备用起来。这篇文章记录一下 Ubuntu 20.04 系统下基础环境搭建的整个过程。

环境搭建过程

升级系统

对于一些某些安装古老系统却又不方便重装系统的机器,需要跨大版本升级:

sudo do-release-upgrade

配置软件源、安装更新

修改镜像源是必须的,需要根据机器地域决定。这里列出几个我常用的:

  • USTC:https://mirrors.ustc.edu.cn/ubuntu/
  • 腾讯云:https://mirrors.cloud.tencent.com/ubuntu/
  • 腾讯云内网:http://mirrors.tencentyun.com/ubuntu/
  • 阿里云:https://mirrors.aliyun.com/ubuntu/

编辑系统源列表文件,将 archive.ubuntu.com 域名替换为上面的即可。

sudo vim /etc/apt/sources.list

随后更新软件列表、安装更新的软件。

sudo apt update
sudo apt upgrade -y

有些软件的更新过程可能需要还需要交互,需要注意一下。

安装常用软件和语言包

安装一些常用软件,虽然大部分可能云主机的镜像都带了。

sudo apt install git wget curl unzip vim -y

安装中文语言包,防止出现奇怪乱码:

sudo apt install language-pack-zh-hant language-pack-zh-hans -y

配置时区

配置系统时区,防止出现奇怪时间问题:

sudo dpkg-reconfigure tzdata

配置 SSH

将本地密钥复制到远程主机:

ssh-copy-id <host_address>

配置远程主机禁用密码登录、禁用 root 用户登录。

sudo vim /etc/ssh/sshd_config

# 添加下面的配置项
PermitRootLogin no
PasswordAuthentication no

重启 SSH 服务以应用设置。

sudo service ssh restart

安装 Docker

Docker 本体

注意参照官方文档:https://docs.docker.com/engine/install/ubuntu/

添加 Docker 官方 GPG 密钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

添加 Docker 镜像源,将命令中 https://download.docker.com/linux/ubuntu 替换为国内源:

  • USTC:http://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu
  • 腾讯云:https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu/
  • 腾讯云内网:https://mirrors.tencentyun.com/docker-ce/linux/ubuntu/
  • 阿里云:http://mirrors.aliyun.com/docker-ce/linux/ubuntu
# 原始命令
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 国内镜像
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] http://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker 本体。

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

安装 Docker Compose

注意参考官方文档:https://docs.docker.com/compose/install/

直接运行以下命令,由于没有镜像,可能会很慢。

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

配置 Docker 容器镜像加速源

参照腾讯云文档:https://cloud.tencent.com/document/product/1207/45596
或阿里云文档:https://help.aliyun.com/document_detail/60750.html

编辑文件 /etc/docker/daemon.json,添加内容:

{
    "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}

重启 Docker 服务

sudo service docker restart

验证 Docker 安装

sudo docker run hello-world

后记

目前准备在这个机器上搭一个 MediaWiki 和一个 CloudLog,估计还得花费一段时间研究研究 Docker 和 Docker Compose 的高级用法。

参考

本作品采用知识共享署名 4.0 国际许可协议进行许可。
本文链接:https://blog.ceba.tech/2021/11/Ubuntu-setting-up/