Ubuntu 镜像实现远程访问的实用方案
一、常见场景与方案选择
二、物理机或虚拟机的远程访问
sudo apt update && sudo apt install -y openssh-server;sudo systemctl start ssh && sudo systemctl enable sshsudo ufw allow 22ssh username@服务器IPsudo apt update && sudo apt install -y xrdp;sudo systemctl start xrdp && sudo systemctl enable xrdpsudo ufw allow 3389sudo useradd -m -s /bin/bash -G sudo user1 && sudo passwd user1sudo apt update && sudo apt install -y tightvncserver xfce4;首次运行 vncserver 设置访问密码~/.vnc/xstartup 末尾加入 startxfce4 &,然后 vncserver -kill :1 再 vncserversudo ufw allow 5901三、Docker 中的 Ubuntu 远程访问
docker run --name ubuntu-ssh -it -p 2060:22 ubuntu bashapt update && apt install -y openssh-server;service ssh startpasswd root(或创建普通用户并赋权)sudo ufw allow 2060ssh -p 2060 root@宿主机IPFROM ubuntu:22.04
RUN apt-get update && apt-get install -y ubuntu-desktop xrdp && apt-get clean
CMD ["/usr/sbin/xrdp", "-n"]
docker build -t ubuntu-desktop .;docker run -d -p 3389:3389 --name my-ubuntu-desktop ubuntu-desktopdocker exec -it my-ubuntu-desktop adduser user1;passwd user1四、从公网访问与内网穿透
docker run --name aubuntu -p 8323:22 ubuntu /bin/bash[docker]
type = tcp
local_ip = 127.0.0.1
local_port = 8323
remote_port = 7222
ssh 用户名@公网服务器IP -p 7222五、常见问题与排查要点
ss -tlnp | grep 端口 检查监听。sudo systemctl restart xrdp)。~/.vnc/xstartup 是否包含 startxfce4 & 并重启 VNC 服务。systemctl status ssh/xrdp)、端口是否正确、用户名与密码/密钥是否有效。