搜索

Linux 初始配置

发布于 · 更新于

此文主要针对 Linux 服务器的配置

但对于 WSL 甚至 Linux 桌面系统,Shell 配置部分大抵也是可以参考的

安全配置

创建普通用户并配置 sudo

假设一开始只创建了 root 用户。那么先以 root 登录

安装 sudo

Terminal window
apt update && apt install -y sudo

创建普通用户

Terminal window
adduser 用户名

会提示创建密码

将用户加入 sudo 组

Terminal window
usermod -aG sudo 用户名

然后测试一下用户登录

ssh 配置

通过编辑 /etc/ssh/sshd_config 来修改配置

这个文件里默认列出的很多选项是被注释掉的状态,取消注释(删掉对应的#)并修改值即可

如: #Port 22 改为 Port 12345

下面的各项都是此文件内的设置项

更改 ssh 端口

非常重要,默认端口绝对会被扫

sshd_config
Port 25565 # 修改成任意非常用端口

改完别忘了去厂商网页控制台调防火墙设置!不然连不上

禁用 root 登录

sshd_config
PermitRootLogin no

配置仅密钥登录

先启用密钥登录

sshd_config
PubkeyAuthentication yes

然后重启 sshd 让新配置生效

Terminal window
sudo systemctl restart ssh

在本地生成 SSH 密钥对并将公钥上传到服务器 注意下面这行指令在 Git Bash 执行( win 没有 ssh-copy-id ),路径表示上有些区别

Terminal window
ssh-copy-id -p 端口 -i /c/Users/Alice/.ssh/公钥.pub 用户名@ip地址

需要注意的是, ssh-copy-id 默认会检查你这个公钥有没有对应的私钥(按它的规则私钥路径应该就是公钥去掉 .pub 后缀),没有会报错。可以 -f 强制安装

测试确认密钥登录正常

可以在 .ssh\config 中定义 Host 别名

比如 ssh -p 2222 -i ~/.ssh/id_ed25519 deploy@10.0.0.3

可以配置为:

Host myserver
HostName 10.0.0.3
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

这样只需 ssh myserver 即可连接

后面 Host * 那一段是可选的优化,为所有 ssh 连接添加保活,以免会话挂一段时间就超时了

确认密钥登陆后再回到服务器的 sshd_config 关闭密码登录,防止被爆破

sshd_config
PasswordAuthentication no

然后重启 sshd 即可

可以使用 sudo sshd -T | grep -i passwordauthentication 检查当前生效的配置情况

关于 fingerprint

初次连接时会提示检查 fingerprint

在服务器里运行

Terminal window
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

可以输出主机的 ed25519 指纹

如果本地(下面指令为 Windows)已经信任服务器了,可以通过

Terminal window
ssh-keygen -lf ~/.ssh/known_hosts

查看指纹

ssh-keygen -R <host> 可以删除信任的指纹

配置防火墙

这个也可以直接问 AI

要点为:

  • 只开放需要用到的几个端口
  • 未开放端口的响应策略设置为 DROP

注意很多云服务器厂商还会需要去网页控制台调节安全组

其他

系统自动更新

Debian 下使用 unattended-upgrades

安装

Terminal window
sudo apt update && sudo apt install unattended-upgrades apt-listchanges

启用

Terminal window
sudo dpkg-reconfigure --priority=low unattended-upgrades

然后验证一下:

Terminal window
cat /etc/apt/apt.conf.d/20auto-upgrades

应该出现:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

然后进行一些配置微调

Terminal window
sudo tee /etc/apt/apt.conf.d/52unattended-upgrades-local >/dev/null <<'EOF'
// 清理不再需要的依赖
Unattended-Upgrade::Remove-Unused-Dependencies "true";
// 需要重启时自动重启
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
EOF

最后再验证一下配置

Terminal window
sudo unattended-upgrade --dry-run -d # 模拟运行
systemctl list-timers 'apt-daily*' # 确认定时器在跑
sudo apt-config dump | grep -E 'Unattended-Upgrade|Periodic' #检查当前生效的配置

防爆破

可以使用 fail2ban 防爆破

Terminal window
sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2ban

Shell 配置

个人偏好 zsh ,同时不使用 oh my zsh 以减少资源占用

如果服务器内存捉襟见肘,可以尝试 ble.sh

安装 zsh 和有用的 zsh 插件

对AI说:

告诉我 <使用的发行版> 如何安装 zsh zsh-syntax-highlighting zsh-autosuggestions
并且告诉我两个插件的完整激活命令( source 路径.zsh )
不需要其它内容

两条激活命令放到下面配置文件对应的位置即可

切换默认 shell

Terminal window
chsh -s $(which zsh)

配置 zsh

~/.zshrc
# 如果是非交互环境则不加载下面的配置
[[ $- != *i* ]] && return
# 加载需要的函数
autoload -Uz compinit && compinit # Tab 补全
autoload -Uz colors && colors # 提供颜色定义变量,下面会用到
# 设置提示符格式
PROMPT='%F{green}%n@%m%f:%F{blue}%~%f %# '
# 设置彩色输出
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
fi
# Tab 补全配置
zstyle ':completion:*' menu select # 按 Tab 时显示候选项列表,可用方向键选择
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} # 候选项使用 ls 同款颜色方案
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # 大小写模糊
# 上下箭头替换成前缀搜索而不只滚动显示历史
zmodload zsh/terminfo
autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
[[ -n ${terminfo[kcuu1]} ]] && bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
[[ -n ${terminfo[kcud1]} ]] && bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
# 第三方插件
# zsh-syntax-highlighting 命令语法高亮
source /安装目录/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# zsh-autosuggestions 根据历史记录提供补全建议
source /安装目录/zsh-autosuggestions/zsh-autosuggestions.zsh
# 历史记录相关设置
HISTFILE=~/.zsh_history # 命令历史保存位置
HISTSIZE=1000 # 内存中保存的条数
SAVEHIST=1000 # 文件中保存的条数
setopt SHARE_HISTORY # 实时与文件同步历史
setopt HIST_IGNORE_ALL_DUPS # 如果新记录与历史中某命令完全相同,则丢弃旧记录
setopt EXTENDED_HISTORY # 在历史中记录命令的执行时间戳
setopt HIST_EXPIRE_DUPS_FIRST # 清理历史时优先删除重复条目
setopt HIST_FIND_NO_DUPS # 历史搜索时不重复显示连续的相同条目
# 其它参数
setopt AUTO_CD # 可以省略 cd ,直接输入目录名进入目录
setopt NO_BEEP # 关闭错误提示音
export LANG=en_US.UTF-8
export EDITOR=vim # 默认编辑器,根据个人喜好调整,如 wsl 可以用 'code --wait'
export VISUAL=vim # 一般与上一行保持一致即可
# 下面再根据你的 .bashrc 加上 PATH 等其它参数(如果有)