Debian 12 系统优化

1. 开启SSH权限

bash

sudo apt update
sudo apt install openssh-server -y
sudo systemctl enable --now ssh

2. 修改SSH端口 & 允许Root登录

bash

sudo nano /etc/ssh/sshd_config

修改以下参数:

conf

Port 2222  # 自定义端口(建议1024-65535)
PermitRootLogin yes  # 允许root登录
PasswordAuthentication yes  # 密码认证

重启服务:

bash

sudo systemctl restart ssh

注意

  • 防火墙需放行新端口:sudo ufw allow 2222

  • 云服务器需在安全组开放端口

3. 修改网卡配置(静态IP)

bash

sudo nano /etc/network/interfaces

配置示例:

conf

auto ens33
iface ens33 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 114.114.114.114

重启网络:

bash

sudo systemctl restart networking

4. 更换国内软件源

bash

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

替换为清华源:

conf

deb 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 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

更新缓存:

bash

sudo apt update && sudo apt upgrade -y

5. 系统更新与清理

bash

sudo apt full-upgrade -y
sudo apt autoremove -y
sudo apt clean

6. 自定义Bash提示符颜色

编辑用户配置:

bash

nano ~/.bashrc

在文件末尾添加:

bash

# 彩色提示符
PS1='\[\e[1;32m\]\u@\h \[\e[1;34m\]\w \[\e[1;31m\]\$ \[\e[0m\]'

立即生效:

bash

source ~/.bashrc

颜色说明

  • 用户名/主机名:绿色

  • 路径:蓝色

  • 提示符:红色


Debian 12 常用命令速查

类别

命令

说明

包管理

sudo apt install <包名>

安装软件

sudo apt remove <包名>

卸载软件

apt search <关键词>

搜索软件包

系统服务

sudo systemctl start <服务名>

启动服务

sudo systemctl enable <服务名>

设置开机自启

文件操作

grep -r "文本" /路径

递归搜索文本

tar -xzvf file.tar.gz

解压tar.gz文件

网络管理

ip addr

查看IP地址

ss -tulnp

查看端口占用

磁盘管理

df -h

查看磁盘空间

du -sh <目录>

查看目录大小

进程管理

htop

交互式进程查看器

kill -9 <PID>

强制结束进程


Debian 系统核心目录结构

text

/
├── bin/        # 基础命令 (ls, cp等)
├── etc/        # 系统配置文件
├── home/       # 用户目录
├── var/        # 动态数据(日志/数据库)
│   └── log/    # 系统日志
├── tmp/        # 临时文件
├── usr/        # 用户程序
│   ├── bin/    # 应用程序命令
│   └── src/    # 源代码
├── boot/       # 启动文件
├── lib/        # 系统库文件
├── opt/        # 第三方软件
├── root/       # root用户目录
└── sbin/       # 超级用户命令

安全增强建议

  1. SSH加固

    • 禁用密码登录:PubkeyAuthentication yes + PasswordAuthentication no

    • 使用Fail2ban防暴力破解

  2. 防火墙设置

    bash

    sudo apt install ufw
    sudo ufw allow 2222  # 只开放必要端口
    sudo ufw enable
  3. 定期更新

    bash

    sudo apt update && sudo apt upgrade -y

通过以上优化,系统将获得更好的性能、安全性和使用体验。生产环境中建议禁用root远程登录,改用普通用户+sudo权限管理。