前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos配置VNC远程

Centos配置VNC远程

原创
作者头像
御坂妹
发布2023-05-28 10:00:00
2.3K0
发布2023-05-28 10:00:00
举报
文章被收录于专栏:御坂网络手记御坂网络手记

摘要

本文主要讲述了Centos如何通安装tigervnc-server,去实现VNC远程连接到Centos。

配置过程

tigervnc-server

先用yum安装tigervnc-server

代码语言:javascript
复制
yum install tigervnc-server

安装完成后,其默认的配置文件在/lib/systemd/system/vncserver@.service,内容如下

代码语言:shell
复制
# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/vncserver@.service
# 2. Replace <USER> with the actual user name and edit vncserver
#    parameters in the wrapper script located in /usr/bin/vncserver_wrapper
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable vncserver@:<display>.service`
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, you should
# limit connections to the local host and then tunnel from
# the machine you want to view VNC on (host A) to the machine
# whose VNC output you want to view (host B)
#
# [user@hostA ~]$ ssh -v -C -L 590N:localhost:590M hostB
#
# this will open a connection on port 590N of your hostA to hostB's port 590M
# (in fact, it ssh-connects to hostB and then connects to localhost (on hostB).
# See the ssh man page for details on port forwarding)
#
# You can then point a VNC client on hostA at vncdisplay N of localhost and with
# the help of ssh, you end up seeing what hostB makes available on port 590M
#
# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
#
# Use "-localhost" to prevent remote VNC clients connecting except when
# doing so through a secure tunnel.  See the "-via" option in the
# `man vncviewer' manual page.


[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver_wrapper <USER> %i
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

/usr/bin/vncserver_wrapper是一个用于启动和停止VNC服务器的脚本,它可以接收以下参数:

代码语言:javascript
复制
•  <USER>:指定运行VNC服务器的用户,需要替换为实际的用户名。
•  %i:指定VNC服务器的显示编号,需要在服务名中用@符号后面的数字或字符串表示,比如vncserver@:1.service。
•  -geometry:指定VNC会话的分辨率,比如-geometry 1280x1024。
•  -kill:停止VNC服务器,需要指定显示编号,比如-kill :1。

配置文件

将默认提供的文件复制到/etc/systemd/system,命令如下:

代码语言:javascript
复制
sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

其中vncserver@:1.service中的:1,是所谓的服务实例名称,这个参数会作为上文配置的i传入,并且该VNC服务的端口号就是5900+i。

然后设置用于VNC登录的用户(这里用oracle作为示例)以及分辨率:

代码语言:shell
复制
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple
User=root    

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l oracle -c "/usr/bin/vncserver %i -geometry 1280x720"
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

要注意的是,这里Serivce中的User要填root, 并且不要设置PID文件路径,不然用systemctl启动该服务时会报错:PID file is not owned by root. Refusing.

完成编辑后重新加载systemctl:

代码语言:javascript
复制
systemctl daemon-reload

设置VNC访问密码

命令行切换到你要VNC登录的账号。执行vncpasswd,设置密码,也可以单独设置一个只能浏览的密码:

代码语言:javascript
复制
vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used

修改防火墙

将VNC的服务开通,当然你也可以选择直接放通端口:

代码语言:javascript
复制
firewall-cmd --add-service=vnc-server --permanent
firewall-cmd --reload

设置开机自启

代码语言:javascript
复制
systemctl enable vncserver@:1.service

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 摘要
  • 配置过程
    • tigervnc-server
      • 配置文件
        • 设置VNC访问密码
          • 修改防火墙
            • 设置开机自启
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档