真机安装 Ubuntu 及基本配置

By | 2022年12月19日

用 rufus 制作 Ubuntu U盘启动盘

家里电脑 CPU X86 架构,系统是 Windows10,下面将它换成 Ubuntu,当前下载的最新版是 ubuntu-22.04.1-desktop-amd64.iso,不同版本安装基本都一样,Ubuntu 官方下载地址

Windows 上装好 Rufus,插入U盘,按下面设置制作启动盘:

装好后,U盘被占 4 个GB,Ubuntu 22 被装在里面了,无需放 iso 镜像。

安装步骤

BIOS 上选择 U盘启动就不讲了,这里最关键的是要 开启CSM选择UEFI

设好 UEFI 重启电脑,就出来下面选项了,选择 Try or Install Ubuntu 开始安装:

装好后,先来设置禁止自动更新:

设置国内原

先备份原来的源:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

接着执行 vi /etc/apt/sources.list 编辑源,依次按 :%d 三个字符清空文件,放入下面内容:

# deb cdrom:[Ubuntu 22.04 LTS _Jammy Jellyfish_ - Release amd64 (20220419)]/ jammy main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted
# deb-src http://security.ubuntu.com/ubuntu jammy-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security universe
# deb-src http://security.ubuntu.com/ubuntu jammy-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security multiverse
# deb-src http://security.ubuntu.com/ubuntu jammy-security multiverse

# This system was installed using small removable media
# (e.g. netinst, live or single CD). The matching "deb cdrom"
# entries were disabled at the end of the installation process.
# For information about how to configure apt package sources,
# see the sources.list(5) manual.

接着使用命令 sudo apt update 更新源就好了。

安装 SSH 服务

# 安装 SSH 服务
apt install -y openssh-server

# 安装 vim(通过 vi 改 ssh 文件太难受)
apt install -y vim

允许 root 账户使用 SSH:

vim /etc/ssh/sshd_config

找到 PermitRootLogin prohibit-password 注释掉,下面加一条 PermitRootLogin yes,改好后重启服务:

systemctl start sshd

生产服务器建议使用SSH公钥认证方式,具体可参考:配置 SSH 公钥认证登录

允许 root 账号登录桌面

默认 root 账号无法登录桌面的,下面来支持下,先配个 root 密码:

sudo passwd root

接下来执行 su root 切换到 root 账户下操作:

vim /etc/pam.d/gdm-autologin

找到 auth required pam_succeed_if.so user !=root quiet_success 加 # 注释,大概在第三行。

vim /etc/pam.d/gdm-password

auth required pam_succeed_if.so user !=root quiet_success 加 # 注释。

两处注释掉后重启电脑,就可以用 root 账号来登录桌面了。

安装 Nvidia GPU 显卡驱动

# 借助下面命令查看可安装驱动
ubuntu-drivers devices

# 在 GTX 3060ti 上装了 recommend 的驱动 nvidia-driver-525-open,重启无效,卸载后,装 nvidia-driver-525 重启就没问题了。
# 经测,GTX 4080 也是装这个驱动就好了。装好后两块显卡可任意插拔,不影响在docker gpu使用。若遇"nvidia-container-cli: initialization error: nvml error: driver not loaded: unknown"很可能是显卡线没插好,我就遇到过2次,搞了好久才回过神来发现线头没插到显卡上。 
sudo apt install  -y nvidia-driver-525

装到一半出现 UEFI Secure boot 验证机制,需要输入一个 secure boot 密码(装 GTX 3060 Ti 时没有出现这个验证机制),以便重启系统时再次输入此密码来验证固件启动的代码是可信的:

安装完显卡驱动后,需重启加载驱动,重启后出现如下图,开始不懂选了第一个 Continue boot,致新安装的 N 卡驱动没有加载,想重新选择重启电脑却再也不不出来此界面:

出现 perform mok management 后正确的做法是:

  1. 当进入蓝色背景的界面perform mok management 后,选择 enroll mok
  2. 进入enroll mok 界面,选择 continue
  3. 进入enroll the key 界面,选择 yes
  4. 接下来输入你在安装驱动时输入的密码
  5. 之后会跳到蓝色背景的界面perform mok management 选择第一个 reboot

这样,重启后N卡驱动就加载了(ubuntu下搜索软件 nvidia):

也可以用下面的命令来测试 nvidia driver 是否安装成功:

nvidia-smi

对于像我一样刚开始 perform mok management 选错的,可以通过下面方式清理驱动,然后重新安装:

# 请注意,这个命令也会卸载掉 nvidia-container-runtime,导致 docker 中无法使用 GPU,必须重新安装才行
sudo apt remove -y --purge nvidia*
sudo apt update
sudo apt install  nvidia-driver-525

GPU 监视

# 每隔1秒更新 GPU 运行情况
watch -n 1 nvidia-smi

Docker

安装 Docker 引擎

按照官方教程一步步复制命令粘贴即可:Install Docker Engine on Ubuntu
装好后默认重启电脑会自动启动 Docker 的。

# 检查 Docker 是否运行正常
systemctl status docker

接着将 Docker 默认数据目录位置 /var/lib/docker 改到 /home/docker,在 /etc/docker/daemon.json 中写入如下内容(文件不存在则新建):

{
    "data-root": "/home/docker"
}

加好后重新启动下服务:

sudo systemctl daemon-reload
sudo systemctl restart docker
# 删除旧目录
rm -rf /var/lib/docker/

安装 nvidia-container-runtime

接着我们再安装 nvidia-container-runtime 插件,以便 docker 容器中可以使用 GPU,安装过程请参考: Docker 中 GPU 的使用

安装 docker-compose

离线安装教程(生产环境推荐)

# 安装最新版(装好后 compose 文件可能会不兼容,得更新)
apt install -y docker-compose
# 查看当前版本
docker compose version
# 卸载
sudo apt-get remove docker-compose-plugin

用 SCP 命令内网传输

若两台 linux 用千兆线连接,推荐直接用 scp 命令在这两台电脑间传输数据,速度可达 125MB/s
若用 Windows 电脑里的 XFTP 等工具与这两台电脑建立连接后,再可视化拖动一台中的数据到另一台,速度会非常慢只有 20MB/s,瓶颈可能是我的 Windows 电脑用的 wifi 把,暂不深入研究了。

#【linux -> linux】
scp /home/docker-image/tomcat-*.tar.gz root@192.168.3.112:/home/docker-image

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注