Ubuntu 命令

VNC 命令

1. 启动 VNC 服务器

vncserver :3 \
  -geometry 1920x1080 \
  -depth 24 \
  -SecurityTypes VncAuth \
  -localhost no
  • :1 指定显示端口号(5901)
  • -geometry 设置分辨率
  • -depth 设置颜色深度
  • -SecurityTypes VncAuth 设置认证方式
  • -localhost no 允许远程连接

简化启动命令

vncserver :1

2. 查看 VNC 会话

vncserver -list

显示所有正在运行的 VNC 会话

3. 停止 VNC 服务器

vncserver -kill :1

停止指定端口的 VNC 会话

4. 修改 VNC 密码

vncpasswd

按提示输入新密码

5. 查看 VNC 进程

ps aux | grep vnc

查看所有 VNC 相关进程

7. 配置 VNC 启动参数

编辑配置文件

~/.vnc/xstartup

示例配置(使用 GNOME 桌面)

#!/bin/bash
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
x-window-manager &

配置后需要添加执行权限

chmod +x ~/.vnc/xstartup

8. 客户端连接 VNC

使用 VNC Viewer 连接

服务器IP:5901
  • 端口号 = 5900 + 显示号(如 :1 对应 5901)

使用 SSH 隧道连接(推荐)

ssh -L 5901:localhost:5901 user@server_ip

然后本地连接 localhost:5901

9. 安装 VNC 服务器

Ubuntu/Debian

sudo apt update
sudo apt install tigervnc-standalone-server tigervnc-common

设置初始密码

vncpasswd

10. 查看 VNC 日志

~/.vnc/*:1.log

查看指定会话的日志文件

SSH 免密码登入

通过配置 SSH 公钥认证,可实现从本机到远程服务器的免密码登录。

1. 在本机生成 SSH 密钥对

若尚未有密钥,可执行:

ssh-keygen -t ed25519 -C "your_email@example.com"

或使用 RSA:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • -t 指定密钥类型(ed25519 更短、更快,推荐)
  • -b RSA 密钥长度(默认 3072)
  • 一路回车则使用默认路径 ~/.ssh/id_ed25519(或 id_rsa)且不设口令

2. 将公钥拷贝到远程服务器

方式一:使用 ssh-copy-id(推荐)

ssh-copy-id user@remote_host

按提示输入一次远程密码,之后公钥会写入远端的 ~/.ssh/authorized_keys

方式二:手动追加公钥

cat ~/.ssh/id_ed25519.pub | ssh user@remote_host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"