在搭建 HomeLab 测试使用过程中,可能会经常创建新的 Ubuntu 虚拟机,或初始化树莓派,记录一些常用的初始化配置
修改主机名
将主机名改为 homelab
hostnamectl hostname homelab
引入 SSH 公钥
- 从本地导入
在本地执行,将公钥复制到要登录的机器上
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
修改 APT 源
将默认的 APT 源替换为清华的源
- 修改镜像源
mv /etc/apt/sources.list /etc/apt/sources.list.backup
sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF"
然后使用 update 验证
apt-get update
安装常用软件
apt install -y --fix-missing curl vim git jq unzip net-tools zsh git-lfs
修改时区
使用 timedatectl
修改 /etc/timezone
文件配置,将时区改为UTC+8
sudo timedatectl set-timezone Asia/Shanghai
安装 zsh
- 安装 zsh 和 git
sudo apt install -y zsh git
- 添加 on-my-zsh
sudo sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
- 添加 autosuggestions 插件
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
- 修改
~/.zshrc
,启用 autosuggestions 插件,将plugins=(git)
替换为以下内容
plugins=(
git
zsh-autosuggestions
)
- 使配置生效
source ~/.zshrc
安装 Docker
- docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
- docker-compose
sudo apt install docker-compose -y
- 配置 docker
mkdir -p ~/.docker && touch ~/.docker/config.json
cat <<EOF > ~/.docker/config.json
{
"auths": {
"registry.xxx.com": {
"auth": "xxx"
}
},
"psFormat": "table {{.ID}}\\t{{.Names}}\\t{{.Status}}\\t{{.Ports}}"
}
EOF
- 安装 loki 日志插件
docker plugin install grafana/loki-docker-driver --alias loki --grant-all-permissions
vim 配置修改
cat <<EOT > /etc/vim/vimrc.local
"修复中文乱码"
:set encoding=utf-8
"语法高亮"
syntax on
"自动对齐"
set autoindent
"检测文件类型"
filetype on
EOT
安装中包
apt install language-pack-zh-hant language-pack-zh-hans -y
配置 ssh
touch /etc/ssh/sshd_config.d/homelab.conf
cat <<EOF > /etc/ssh/sshd_config.d/homelab.conf
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes
EOF
- 重启 SSH 服务
systemctl restart ssh.service