在Linux上配置远程访问可以通过多种协议实现,最常用的是SSH(安全Shell)和VNC(虚拟网络计算)。以下是详细的配置方法:
SSH 是加密的远程命令行管理协议,默认使用22端口。
# Ubuntu/Debian
sudo apt update
sudo apt install openssh-server
# CentOS/RHEL
sudo yum install openssh-server
sudo systemctl start sshd # 启动服务
sudo systemctl enable sshd # 设置开机自启
sudo systemctl status sshd
# Ubuntu/Debian (ufw)
sudo ufw allow 22/tcp
# CentOS/RHEL (firewalld)
sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload
从客户端使用以下命令连接:
ssh username@服务器IP地址
/etc/ssh/sshd_config
,修改 Port 2222
,重启服务。PermitRootLogin no
。ssh-keygen
生成密钥对,禁用密码登录。适用于图形界面环境(如Ubuntu Desktop)。
# Ubuntu/Debian
sudo apt install tightvncserver
# CentOS/RHEL
sudo yum install tigervnc-server
vncserver # 首次运行会提示设置密码
创建服务文件 /etc/systemd/system/vncserver@.service
,内容示例:
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=your_username
ExecStart=/usr/bin/vncserver :1 -geometry 1920x1080 -depth 24
ExecStop=/usr/bin/vncserver -kill :1
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start vncserver@1
sudo systemctl enable vncserver@1
sudo ufw allow 5901/tcp # 默认端口5900+:1
使用VNC客户端(如RealVNC、TigerVNC)连接:
服务器IP地址:1
RDP(xrdp)
适合Windows用户习惯:
sudo apt install xrdp # Ubuntu
sudo systemctl enable xrdp
WebSSH(通过浏览器)
使用工具如 shellinabox
或 wetty
。
AllowUsers
配置。sudo apt upgrade openssh-server
。ss -tulnp | grep ssh
)。根据需求选择协议,SSH适合管理,VNC/RDP适合图形操作。