Linux 中的常用初始化操作

新开的服务器想要用得好,一些初始化操作就少不了。
对于非 root 账户登录的情况,这些命令可能需要加上 sudo
(这部分命令均以自用为主要目的,仅供参考

调整用户(组)

新设备到位,用户和用户组总是最重要的

新建用户

1
adduser <username>

修改密码

1
passwd [username]

加入用户组

1
usermod <username> -aG <usergroup>

重命名用户

编辑/etc/passwd /etc/shadow /etc/group这三个文件并重命名home目录。

修改 SSH 设置(安全第一)

安全性参考方案:仅启用密钥登录 + 换到非 22 端口
请先su到你需要使用 SSH 的账户下再进行如下操作

SSH 密钥对

生成

1
2
cd ~
ssh-keygen

然后一路回车就好,直到得到类似提示

1
2
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

这时就生成成功了
私钥文件是/root/.ssh/id_rsa
公钥文件是/root/.ssh/id_rsa.pub

放置、改权限

1
2
mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa

把显示的私钥复制下来并保存

SSH 配置文件

文件位置:/etc/ssh/sshd_config
可以用vimnano打开编辑

1
vim /etc/ssh/sshd_config

1
nano /etc/ssh/sshd_config

如果出现找不到命令的话可以看后文的软件包处理部分

需要修改一下参数 (行数仅供参考)
13 行 #Port 22 改为 Port 80
改变 SSH 的端口,80 可以换成你想要的任意端口 (只要不冲突就行)
38 行 #PubkeyAuthentication no 改为 PubkeyAuthentication yes
57 行 #PasswordAuthentication yes 改为 PasswordAuthentication no
开启密钥登录,关闭密码登录
默认不允许 root 登录的系统,可以在配置文件末尾加上这行来解决

1
PermitRootLogin yes

修改主机名

1
hostnamectl set-hostname <NAME>

个人习惯,也不多说,下一个

改变系统语言为中文

Ubuntu LTS:

1
dpkg-reconfigure locales

使用Page Down切到列表最底下,上下移动并用空格选中zh_CN.GBK GBKzh_CN.UTF-8 UTF-8两项,回车。
示意图1

选择zh_CN.UTF-8 UTF-8为默认语言,回车。
示意图2
因为我改过了,所以我的界面是中文的

修改语言需要重启后生效!

SELinux 配置

什么是 SELinux?

安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。
如果可以熟练掌握 SELinux 并正确运用,我觉得整个系统基本上可以到达” 坚不可摧” 的地步了(请永远记住没有绝对的安全)。
引用自:一文彻底明白 linux 中的 selinux 到底是什么 - PHPYuan

然而因为我没能力搞定他,所以我一般会关掉。

查看状态

1
getenforce
显示值 状态
enforcing 强制模式 违反 SELinux 规则的行为将被阻止并记录到日志中
permissive 宽容模式 违反 SELinux 规则的行为只会记录到日志中
disabled 关闭
Command not found 未安装 SELinux

如果想从 disabled 切换到 enforcing 或者 permissive 的话,需要重启系统
enforcing 和 permissive 模式可以通过 setenforce 1|0 命令快速切换。

永久改变状态

编辑/etc/selinux/config
SELINUX=这一项改成你想要的状态
该重启就重启

软件源

这一部分真的是因人而异,不要无脑跟着操作

更换国内软件源

部分服务商镜像提供了自有源,无需手动更换
推荐清华大学 TUNA 镜像源阿里镜像源
官方有操作引导,不多说了

安装常用软件 (源)

常用软件

1
2
apt remove ufw iptables unattended-upgrades # 直接使用 nft
apt install -y nftables screen wget vim htop curl lrzsz zsh git bind9-dnsutils bird2 wireguard-tools iperf3 whois conntrack containerd linux-generic-hwe-24.04 linux-headers-generic-hwe-24.04 linux-image-generic-hwe-24.04

Ubuntu Pro

https://ubuntu.com/pro/dashboard

nerdctl

1
2
3
4
5
6
7
8
9
10
mkdir -p /tmp/tmp && cd /tmp/tmp

NERDCTL_VERSION=$(curl -s https://api.github.com/repos/containerd/nerdctl/releases/latest | grep tag_name | cut -d '"' -f 4)
NERDCTL_VERSION=${NERDCTL_VERSION#v}
wget -c https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz
tar -xzf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz

mv nerdctl /usr/local/bin/
systemctl enable --now containerd
nerdctl version

如果需要 CNI 网络插件支持,可以安装 cni-plugins

1
2
3
4
5
6
7
8
9
mkdir -p /tmp/tmp && cd /tmp/tmp

CNI_VERSION=$(curl -s https://api.github.com/repos/containernetworking/plugins/releases/latest | grep tag_name | cut -d '"' -f 4)
CNI_VERSION=${CNI_VERSION#v}

wget -c https://github.com/containernetworking/plugins/releases/download/v${CNI_VERSION}/cni-plugins-linux-amd64-v${CNI_VERSION}.tgz

mkdir -p /opt/cni/bin
tar -C /opt/cni/bin -xzf cni-plugins-linux-amd64-v${CNI_VERSION}.tgz

再来个zsh

1
2
3
4
5
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

chsh -s $(which zsh)

然后修改 ~/.zshrc 文件,将 plugins=(git) 改为 plugins=(git extract zsh-autosuggestions zsh-syntax-highlighting)

主题选择

常用环境使用 powerlevel10k/powerlevel10k 主题

1
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

root 环境使用 dpoggi 主题

更新到最新版

1
2
apt update
apt full-upgrade -y

sysctl

参照 https://github.com/dn-11/scripts/blob/master/sysctl.conf 微调。

DN11

1
2
3
4
5
6
7
8
9
10
services:
uptime-kuma:
image: louislam/uptime-kuma:1
container_name: uptime-kuma
volumes:
- ./data:/app/data
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
network_mode: host

对应的 /etc/systemd/system/uptime-kuma.service 服务文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Uptime Kuma Service
After=containerd.service

[Service]
Type=simple
WorkingDirectory=/root/uptime-kuma
ExecStart=/usr/local/bin/nerdctl compose -f /root/uptime-kuma/compose.yaml up
ExecStop=/usr/local/bin/nerdctl compose -f /root/uptime-kuma/compose.yaml down
Restart=no

[Install]
WantedBy=multi-user.target

End?