LIVE_STREAM_CONNECTED
Linux 新服务器基础配置指南
A
Mastermind
admin
Dimension
Linux
Timestamp
2025年12月21日
新装 Linux 服务器后的必备工具安装和基础命令,以 Ubuntu/Debian 为主。
系统更新
# 更新软件源
sudo apt update
# 升级已安装软件
sudo apt upgrade -y
# 完整升级(包括内核)
sudo apt full-upgrade -y
# 清理无用包
sudo apt autoremove -y
必备工具安装
基础工具包
# 常用工具一键安装
sudo apt install -y \
curl \
wget \
git \
vim \
htop \
tree \
unzip \
zip \
net-tools \
lsof \
screen \
tmux \
build-essential \
software-properties-common
各工具说明
| 工具 | 用途 |
|---|---|
| curl/wget | 下载文件、请求接口 |
| git | 版本控制 |
| vim | 文本编辑 |
| htop | 进程监控(比 top 好用) |
| tree | 目录树形显示 |
| unzip/zip | 压缩解压 |
| net-tools | 网络工具(ifconfig 等) |
| lsof | 查看端口占用 |
| screen/tmux | 终端复用 |
| build-essential | 编译工具链 |
用户管理
创建新用户
# 创建用户
sudo adduser username
# 添加 sudo 权限
sudo usermod -aG sudo username
# 切换用户
su - username
# 删除用户
sudo userdel -r username
SSH 密钥配置
# 本地生成密钥
ssh-keygen -t ed25519 -C "your@email.com"
# 复制公钥到服务器
ssh-copy-id user@server_ip
# 或手动添加
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "your_public_key" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
禁用密码登录
编辑 /etc/ssh/sshd_config:
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
重启 SSH:
sudo systemctl restart sshd
防火墙配置
UFW(推荐)
# 安装
sudo apt install ufw
# 默认规则
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许端口
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 3000
# 启用
sudo ufw enable
# 查看状态
sudo ufw status
# 删除规则
sudo ufw delete allow 3000
常用命令
文件与目录
# 查看目录
ls -la
tree -L 2
# 磁盘空间
df -h # 磁盘使用
du -sh * # 当前目录各文件大小
du -sh /var # 指定目录大小
# 查找文件
find / -name "*.log"
find . -type f -mtime +30 # 30天前的文件
# 文件内容
cat file.txt
less file.txt
tail -f /var/log/syslog # 实时日志
head -n 20 file.txt
进程管理
# 查看进程
ps aux
ps aux | grep nginx
htop
# 杀死进程
kill PID
kill -9 PID # 强制
pkill nginx # 按名称
# 后台运行
nohup command &
nohup node app.js > output.log 2>&1 &
网络相关
# 查看端口
netstat -tlnp
ss -tlnp
lsof -i :80
# 网络连接
curl -I https://example.com
wget https://example.com/file.zip
ping google.com
# IP 地址
ip addr
hostname -I
系统信息
# 系统版本
cat /etc/os-release
uname -a
# 硬件信息
free -h # 内存
nproc # CPU 核心数
lscpu # CPU 详情
# 运行时间
uptime
服务管理
# systemctl 管理服务
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl status nginx
sudo systemctl enable nginx # 开机自启
sudo systemctl disable nginx
环境安装
Node.js (nvm)
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
# 安装 Node.js
nvm install --lts
nvm use --lts
node -v
Python
# 安装 Python 3
sudo apt install python3 python3-pip python3-venv
# 创建虚拟环境
python3 -m venv myenv
source myenv/bin/activate
Docker
# 快速安装
curl -fsSL https://get.docker.com | sh
# 添加当前用户到 docker 组
sudo usermod -aG docker $USER
# 安装 docker-compose
sudo apt install docker-compose-plugin
时区与时间
# 查看时区
timedatectl
# 设置时区
sudo timedatectl set-timezone Asia/Shanghai
# 同步时间
sudo apt install chrony
sudo systemctl enable chrony
交换空间
# 创建 2G 交换文件
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 永久生效
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# 查看
free -h
定时任务
# 编辑 crontab
crontab -e
# 示例
# 每天凌晨 3 点执行
0 3 * * * /path/to/script.sh
# 每 5 分钟执行
*/5 * * * * /path/to/script.sh
# 查看任务
crontab -l
安全加固
Fail2ban
# 安装
sudo apt install fail2ban
# 配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vim /etc/fail2ban/jail.local
# 启动
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
自动安全更新
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
总结
新服务器配置清单:
- 系统更新
- 安装基础工具
- 创建普通用户,配置 SSH 密钥
- 禁用密码登录和 root 登录
- 配置防火墙
- 设置时区
- 安装运行环境
- 配置安全工具
按需选择,保持服务器安全稳定。