Debian 服务器配置备忘
Kecho

记录个人 Debian 服务器的配置过程。

软件安装

apt

debian | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

编辑配置文件:

1
2
sudo - root
vi /etc/apt/sources.list

替换为:

1
2
3
4
5
6
7
8
9
10
11
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

其中 main contrib non-free non-free-firmware 表示不同类型的软件包。

  • main 表示符合 Debian Free Software Guidelines 的包;
  • contrib 是依赖一些非自由软件就才能正常运作的开源软件;
  • non-free 和 non-free-firmware 表示非自由软件。

上述这种格式称为传统格式(又称 One-Line-Style 格式)。

新的 DEB822 格式自 APT 1.1(2015 年发布)起支持,文件后缀为 .sources,存储在 /etc/apt/sources.list.d/ 目录下,格式类似如下:

1
2
3
4
5
6
7
8
9
10
11
Types: deb deb-src
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
Suites: bookworm bookworm-updates bookworm-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

Types: deb deb-src
URIs: http://security.debian.org/debian-security
Suites: bookworm-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

安装常用工具:

1
2
apt update
apt install vim git wget curl

常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
apt update    # 更新软件索引
apt upgrade # 更新系统中的所有软件包,但不安装额外的软件包或卸载软件包
apt full-upgrade # 更新系统中的所有软件包,并且在必要的时候安装额外的软件包或卸载软件包
apt install foo # 安装软件包 foo 和它的全部依赖
apt remove foo # 卸载软件包 foo
apt purge foo # 卸载软件包 foo 和它的配置文件
apt list --upgradable # 列出所有可以更新到新版本的软件包

apt search foo # 找出描述中包含 foo 的软件包
apt show foo # 打印软件包的详细信息
apt-cache depends foo # 打印软件包的依赖
apt-cache showpkg foo # 打印软件包各可用版本的详细信息,以及反向依赖它的软件包

dpkg

除了使用 apt 软件管理工具安装软件外,还可以直接使用 dpkg 命令本地安装 deb 软件包

deb 包安装与卸载:

1
2
3
4
5
6
7
dpkg -i hello.deb       # 安装
dpkg -r hello.deb # 保留配置文件卸载
dpkg -P hello.deb # 清除配置文件卸载

dpkg -l # 显示已安装程序列表
dpkg -l | grep "hello" # 查询已安装包
dpkg -l | grep ^rc # 查看处于 rc 状态的软件包

dpkg -l 显示已安装程序列表,其中每行输出的第一列 ii 表示软件包的安装和配置状态,其格式如下:期望状态|当前状态|错误

期望状态有以下几种:

  • u:即 unknown,软件包未安装且用户未请求安装
  • i:即 install,用户请求安装该软件包
  • r:即 remove,用户请求卸载该软件包
  • p:即 purge,用户请求卸载该软件包并清理配置文件
  • h:即 hold,用户请求保持续当前软件包版本

当前状态有以下几种:

  • n:即 not-installed,软件包未安装
  • i:即 installed,软件包已安装并完成配置
  • c:即 config-files,软件包已经被卸载,但是其配置文件未清理
  • u:即 unpacked,软件包已经被解压缩,但还未配置
  • f:即 half-configured,配置软件包时出现错误
  • w:即 triggers-awaited,触发器等待
  • t:即 triggers-pending,触发器未决

错误状态有以下几种:

  • h:软件包被强制保持
  • r:即 reinstall-required,需要卸载并重新安装
  • x:软件包被破坏

因此 ii 表示该软件需要安装且已经安装,没有出现错误;iu 表示已经安装该软件,但未正确配置;rc 表示该软件已经被删除,但配置文件未清理。

一个 deb 包的目录结构如下:

  • my.deb
    • DEBIAN
      • control (存放软件包的作者、版本、描述等信息)
      • preinst (安装前执行的脚本)
      • postinst (安装后执行的脚本)
      • prerm (卸载前执行的脚本)
      • postrm (卸载后执行的脚本)
    • usr (存放源码)
      • xxxxxx

可以使用以下命令对 deb 包进行解压和打包:

1
2
3
dpkg -x my.deb build  # 解压源码
dpkg -e my.deb build # 解压其中的 DEBIAN 内容
dpkg -b build my.deb # 重新打包为 deb

sudo

安装:

1
2
su - root
apt install sudo

配置:

1
usermod -aG sudo username #将用户 username 添加到 sudo 组中

或者使用 visudo 命令或者编辑 /etc/sudoers 文件,文中添加 username ALL=(ALL) ALL ,保存文件后注销重新登录。

docker

docker-ce | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

卸载:

1
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do apt-get remove $pkg; done

信任公钥并添加仓库:

1
2
3
4
5
6
7
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null

安装:

1
2
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

编辑配置文件:

1
sudo vim /etc/docker/daemon.json

增加代理加速地址:

1
2
3
4
5
6
7
{
"registry-mirrors": [
"https://docker.1panel.live",
"https://docker.ketches.cn",
"https://hub.iyuu.cn"
]
}

服务启动:

1
sudo systemctl restart docker

常用命令:

1
2
3
4
5
6
7
sudo vim docker-compose.yml # 编辑容器配置文件
sudo docker compose up -d
sudo docker compose down
sudo docker exec -it [contianer_name] /bin/bash
sudo docker images # 列出所有镜像
sudo docker image remove xxx:xxx # 删除指定镜像
sudo docker container ps # 列出在运行容器

网络配置

编辑配置文件:

1
sudo vim /etc/network/interfaces

增加内容:

1
2
3
4
5
6
7
auto enp0s3
iface enp0s3 inet static
address 192.168.1.240/24
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 223.5.5.5

服务启动:

1
sudo systemctl restart networking.service

常用命令:

1
2
ip address # 查看网络接口
ip route # 查看路由

smb

安装:

1
2
sudo apt update
sudo apt install samba

配置:

1
2
mkdir share # 创建共享文件夹
sudo vim /etc/samba/smb.conf # 编辑配置文件

末尾增加:

1
2
3
4
5
6
[SHARE]
comment = My Share Folder
path = /home/username/share
writable = yes
browseable = no
guest ok = no

服务启动:

1
sudo systemctl restart smbd.service

crontab

Debian 12 已经默认安装了 cron

安装:

1
2
sudo apt update
sudo apt install cron

常用命令:

1
2
3
4
5
6
7
crontab –e     //修改 crontab 文件,如果文件不存在会自动创建
crontab –l //显示 crontab 文件
crontab -r //删除 crontab 文件
crontab -ir //删除 crontab 文件前提醒用户

# 默认使用当前用户,可以使用 -u 指定用户
crontab [-u username] -e

frpc 自启动

创建配置文件:

1
sudo vim /etc/systemd/system/frpc.service

添加内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Frpc Service
After=network.target

[Service]
Type=idle
ExecStart=/home/kecho/frp/frpc -c /home/kecho/frp/frpc.toml
Restart=on-failure
RestartSec=30s
User=nobody

[Install]
WantedBy=multi-user.target

服务启动:

1
2
sudo systemctl daemon-reload
sudo systemctl enable frpc.service
 评论
评论插件加载失败
正在加载评论插件
总字数 29.4k