温馨提示×

如何在Linux Minimal中进行远程管理

小樊
38
2025-11-22 13:55:29
栏目: 智能运维

Linux Minimal 远程管理实操指南

一、首选方式 SSH 远程管理

  • Ubuntu Minimal
    • 安装与启动:执行 sudo apt update && sudo apt install -y openssh-server,随后 sudo systemctl enable --now ssh(或 sudo systemctl start ssh)。检查状态:sudo systemctl status ssh
    • 连接:在客户端执行 ssh username@server_ip
  • CentOS Minimal
    • 安装与启动:执行 sudo yum install -y openssh-server,随后 sudo systemctl enable --now sshd。检查状态:sudo systemctl status sshd
    • 连接:在客户端执行 ssh username@server_ip
  • 防火墙放行
    • Ubuntu(UFW):sudo ufw allow 22/tcp;如改用其他端口(如 2222),放行对应端口:sudo ufw allow 2222/tcp
    • CentOS(firewalld):sudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload;如更改端口,放行端口号:sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reload
  • 获取服务器 IP
    • 执行 ip addr showhostname -I 查看可用地址。

二、图形化远程桌面方案

  • Ubuntu Minimal
    • VNC(x11vnc):安装 sudo apt-get install -y x11vnc,设置密码 x11vnc -storepasswd;创建服务文件 /etc/systemd/system/x11vnc.service(示例:/usr/bin/x11vnc -display :0 -auth /home/your_username/.Xauthority -forever -loop -noxdamage -repeat -rfbauth /home/your_username/.vnc/passwd -rfbport 5900 -shared),随后 sudo systemctl enable --now x11vnc。客户端用 5900 端口连接。
    • 替代 VNC(TigerVNC):安装 sudo apt-get install -y tigervnc-standalone-server,设置密码 vncpasswd,启动会话 vncserver :1,默认端口 5901;如需放行:sudo ufw allow 5901/tcp
    • RDP(XRDP):安装 sudo apt-get install -y xrdp,启动 sudo systemctl enable --now xrdp,Windows 使用远程桌面连接 3389 端口。
  • CentOS Minimal
    • VNC(TigerVNC):安装 sudo yum install -y tigervnc-server tigervnc-common,设置密码 vncpasswd,启动会话 vncserver :1 -geometry 1920x1080;创建 /etc/systemd/system/vncserver@.service(设置 UserPIDFile、ExecStart 等),随后 sudo systemctl daemon-reload && sudo systemctl enable --now vncserver@1.service。客户端连接 5901 端口。

三、安全加固要点

  • 禁用 root 直连:编辑 /etc/ssh/sshd_config,设置 PermitRootLogin no
  • 改用密钥登录:本地生成密钥 ssh-keygen,将公钥追加到服务器 ~/.ssh/authorized_keys,在 sshd_config 中设置 PasswordAuthentication noPubkeyAuthentication yes,重启 sshd
  • 更改默认端口:在 sshd_config 中设置 Port 2222,同时防火墙放行新端口,客户端使用 ssh -p 2222 user@ip
  • 限制可登录用户:在 sshd_config 中使用 AllowUsers youruser,仅允许指定账户登录。

四、常见问题排查

  • 服务未运行:检查 sudo systemctl status sshsudo systemctl status sshd,必要时 sudo systemctl restart ssh/sshd
  • 端口未放行:确认 UFW/firewalld 已放行 22/2222/5901/3389 等对应端口。
  • 客户端连接被拒:核对 IP端口用户名;如使用非默认端口,客户端需加 -p 端口
  • VNC 灰屏或黑屏:确认已安装并运行桌面环境(Minimal 默认无 GUI,需先安装桌面或仅使用 SSH)。

0