温馨提示×

Debian Minimal如何远程访问服务器

小樊
42
2026-01-05 11:43:06
栏目: 云计算

Debian Minimal 远程访问服务器实操指南

一、首选方式 SSH 远程登录

  • 安装与启动
    • 更新索引并安装服务端:sudo apt update && sudo apt install -y openssh-server
    • 启动并设置开机自启:sudo systemctl enable --now ssh(部分系统服务名为 sshd,两者二选一)
  • 防火墙放行
    • UFW:sudo ufw allow 22/tcp
    • firewalld:sudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload
  • 连接测试
    • 本地或局域网:ssh 用户名@服务器IP
    • 云服务器:ssh 用户名@公网IP -p 22
  • 常用排错
    • 查看服务状态:systemctl status ssh(或 sshd)
    • 查看监听端口:ss -tnlp | grep :22
    • 客户端调试:ssh -vvv 用户名@IP 观察握手与认证过程

二、SSH 安全加固与进阶配置

  • 修改默认端口:编辑 /etc/ssh/sshd_config,将 Port 22 改为如 2222,重启服务后连接需加 -p 2222
  • 禁用 root 登录:PermitRootLogin no
  • 仅用公钥认证(推荐)
    • 客户端生成密钥:ssh-keygen -t ed25519 -C “email”
    • 上传公钥:ssh-copy-id -p 22 -i ~/.ssh/id_ed25519.pub 用户@IP
    • 服务端关闭密码:PasswordAuthentication no;PubkeyAuthentication yes
    • 权限要求:~/.ssh 700;~/.ssh/authorized_keys 600
  • 多因素认证 MFA(TOTP)
    • 安装:sudo apt install -y libpam-google-authenticator
    • 运行 google-authenticator 生成密钥与紧急码
    • PAM 配置:/etc/pam.d/sshd 增加 auth required pam_google-authenticator.so
    • SSH 配置:ChallengeResponseAuthentication yes;AuthenticationMethods publickey,keyboard-interactive
  • 日志与防护
    • 日志:tail -f /var/log/auth.log
    • 防暴力:sudo apt install -y fail2ban,按默认配置启用即可

三、图形化远程桌面可选方案

  • VNC(轻量,适合最小化安装)
    • 安装:sudo apt install -y tightvncserver
    • 首次启动:vncserver :1(设置访问密码)
    • 连接:VNC Viewer 输入 服务器IP:5901(显示号 n 对应端口 5900+n
  • xrdp(RDP,Windows 远程桌面友好)
    • 安装桌面与服务:sudo apt install -y xrdp xfce4
    • 启动:sudo systemctl enable --now xrdp
    • 连接:Windows 远程桌面 mstsc 输入 服务器IP:3389
  • 浏览器管理面板 Cockpit(可选)
    • 安装:sudo apt install -y cockpit
    • 启用并访问:sudo systemctl enable --now cockpit.socket,浏览器打开 https://服务器IP:9090

四、最小化系统联网与软件源配置

  • 若 apt 无法安装软件(如 openssh-server),先配置可用软件源
  • 备份并编辑源:sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • 使用国内镜像(示例为 Debian 12/Bookworm,按需替换版本代号)
    • deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
    • deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
    • deb https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
  • 更新索引并安装:sudo apt update && sudo apt install -y openssh-server

0