VirtualBox中CentOS 7执行code .无法打开VSCode求助
解决CentOS 7 Minimal下VSCode启动失败的问题
报错原因
CentOS 7 Minimal镜像默认未安装X Window图形环境,桌面版VSCode依赖X服务器提供图形渲染,因此启动时触发Missing X server or $DISPLAY错误。
解决方案
方案1:安装本地图形桌面环境
若需在服务器本地使用VSCode图形界面,安装GNOME桌面:
- 安装GNOME桌面组:
yum groupinstall "GNOME Desktop" - 设置系统默认启动图形界面:
systemctl set-default graphical.target - 重启服务器,进入图形桌面后即可正常启动VSCode。
方案2:通过SSH X转发远程显示VSCode界面
本地机器有图形环境时,可通过SSH X转发将VSCode界面显示在本地:
- 服务器端安装xauth工具:
yum install xauth - 本地终端用带X转发参数的SSH命令连接服务器:
ssh -X your-username@server-ip # 或使用-Y参数(信任X转发连接) ssh -Y your-username@server-ip - 连接成功后,在SSH会话中执行
code命令,VSCode界面将显示在本地图形环境中。
方案3:使用VSCode Server(推荐,无需图形环境)
VSCode Server通过浏览器访问,无需服务器端图形环境:
- 安装curl工具:
yum install curl - 执行官方安装脚本:
curl -fsSL https://code-server.dev/install.sh | sh - 启动code-server服务:
systemctl start code-server@$USER - 设置开机自启:
systemctl enable code-server@$USER - 本地浏览器访问
http://server-ip:8080,查看~/.config/code-server/config.yaml中的密码登录即可使用。
内容的提问来源于stack exchange,提问作者Joseph Park
相关产品推荐
相关产品推荐

