VNC

VNC简介

vnc是Virtual Network Computing(虚拟网络计算机)的缩写。VNC是由AT&T的欧洲研究实验室开发的一款优秀的跨平台远程桌面控制软件,支持linux,unix,windows操作系统跨平台远程桌面控制。VNC有两部分组成,分别是:服务端([vnc](http://www.aiezu.com/tag/vnc)server)和客户端(vncviewer)

VNC安装

# 提前安装图形服务

# 列出的组列表里有GNOME Desktop。
yum grouplist  
#安装之
yum groupinstall -y "GNOME Desktop" 
# 安装完成后,修改默认启动方式为图形化界面
systemctl set-default graphical.target  //设置成图形模式 
# 如果要换回来 
systemctl set-default multi-user.target  //设置成命令模式 
#然后重启系统即可
# 以root用户运行以下命令来安装vncserver
	yum install tigervnc-server 

# 同样运行以下命令来安装vncviewer
	yum install vnc
# vnc连接
	192.168.0.10:5901  #直接可以连上某个用户,因为只有一个用户三个端口(默认),不会重复

VNC配置

VNC的运行机制介绍

​ 在配置VNC前,必须了解VNC的运行机制。Linux下的VNC可以同时启动多个vncserver,各个vncserver之间用显示编号(display number)来区分,每个vncserver服务监听3个端口,它们分别是

  • 5800+显示编号: VNC的httpd监听端口,如果VNC客户端为IE,Firefox等非vncviewer时必须开放
  • 5900+显示编号: VNC服务端与客户端通信的真正端口,必须无条件开放
  • 6000+显示编号: X监听端口,可选
VNC-vncservers

​ 显示编号、开放的端口分别由/etc/sysconfig/vncservers文件中的VNCSERVERS和VNCSERVERARGS控制。VNCSERVERS的设置方式为:

# VNCSERVERS="显示编号1:用户名1 …"
	VNCSERVERS="1:root 2:aiezu"

# VNCSERVERARGS的设置方式为:
	VNCSERVERARGS[显示编号1]="参数一 参数值一 参数二 参数值二 ……"
	VNCSERVERARGS[2]="-geometry 800x600 -nohttpd"

# VNCSERVERARGS的详细参数有:
    -geometry 桌面分辨率,默认1024x768;
    -nohttpd 不监听HTTP端口(58xx端口);
    -nolisten tcp 不监听X端口(60xx端口);
    -localhost 只允许从本机访问;
    -AlwaysShared 默认只同时允许一个vncviewer连接,此参数允许同时连多个vncviewer;
    -SecurityTypes None 登录不需要密码认证VncAuth默认值,要密码认证

# 修改/etc/sysconfig/vncserver文件:本例我们开启两个vncserver,分别是root用户,显示编号为1和用户aiezu,显示编号为3,并且全不开启X监听端口60xx
    VNCSERVERS="1:root 3:aiezu"
    VNCSERVERARGS[1]="-geometry 800x600 -nolisten tcp"
    VNCSERVERARGS[3]="-geometry 1024x768 -nolisten tcp"
# 设置VNC用户密码,接下来设置VNC的密码,此步骤不可跳过,否则VNC Server将无法启动,在Linux Shell下执行下列命令
# su - aiezu
# vncpasswd
Password:
Verify:
# su - root
# vncpasswd
Password:
Verify:
service vncserver start //启动vncserver

​ 运行上面命令后,会在用户根目录($HOME)下的".vnc"文件夹下生成一系列文件其中passwd为vnc用户密码文件,由vncpasswd生成。其他的都由vnc初次启动时生成,xstartup为VNC客户端连接时启动的脚本

# 修改".vnc/xstartup"文件,执行到上面步骤后,VNC Server已经能正常运行。但是默认设置下,客户连接时启动的是xterm,我们如果想看到桌面,必须将用户根目录下的".vnc/xstartup"文件中的最后两行注释掉,然后根据你安装的桌面坏境,添加一行"startkde &"或者"gnome-session &",如下:

#!/bin/sh
# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
startkde &
# gnome-session &

​ 配置完各个用户根目录下的".vnc/xstartup"后,执行service vncserver restart 重新启动vncserver使配置生效

Service
# 官方配置文件
[root@init-mysql ~]# cat /lib/systemd/system/vncserver@.service 
# 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
# 拷贝官方的
	cp /usr/lib/systemd/system/vncserver@.service /usr/lib/systemd/system/vncserver@:1.service

# 内容

[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 root %i
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

# 设置vnc密码
	vncpasswd

# 启动服务:
	systemctl enable vncserver@\:1.service
防火墙
# centos6
cat /etc/sysconf/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5931 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5932 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5933 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5934 -j ACCEPT

# 重启防火墙
service iptables restart

VNC实例

VNC免密参考:https://askubuntu.com/questions/328240/assign-vnc-password-using-script

VNC免密-实例一
#!/usr/bin/expect  #第一行必不可少,表明调用expect
set timeout 10
spawn vncpasswd    #spawn是expect中的命令,调用vncpasswd命令
expect "Password:" #expect当遇到“Password”时
send "123456\n"    #送入“123456”并回车,windows中是\r\n,linux中只需\n
expect "Verify:"
send "123456\n"
interact           #返回终端

如果要使用echo 把它写入一个文件中,则应当注意对 !"号的处理
输出!号 需要用 echo ''单引号
输出" 号 需要用  \" 进行转义
==================================
echo '#!/usr/bin/expect' >> /root/vncpasswdshell.sh
echo "set timeout 10" >> /root/vncpasswdshell.sh
echo "spawn vncpasswd" >> /root/vncpasswdshell.sh
echo "expect \"Password:\"" >> /root/vncpasswdshell.sh
echo "send \"123456\n\"" >> /root/vncpasswdshell.sh
echo "expect \"Verify:\"" >> /root/vncpasswdshell.sh
echo "send \"123456\n\"" >> /root/vncpasswdshell.sh
echo "interact" >> /root/vncpasswdshell.sh
chmod +x /root/vncpasswdshell.sh
cd /root
./vncpasswdshell.sh
=======================
interact会返回交互状态,从而使接下来的命令暂停执行。
如果想一路顺下去执行。可以采用expect eof;exit
从而退出!#/usr/bin/expect脚本
# 方法一
[root@localhost 桌面]# cat test.sh 
#!/bin/bash
echo $$
cat << EOF | bash
#!/bin/bash
echo $$
su test -
set timeout 5
expect -c '
spawn vncpasswd
expect "Password:"
send "admin123\r"    
expect "Verify:"
send "admin123\r"'
id             # 这里显示是test用户
EOF
id			   # 这里显示是root用户
# Usage: $ ./scriptname <filename> <password>   #filename 应该要是passwd才会生效
#!/bin/sh    
myuser="$1"
mypass="$2"
/usr/bin/expect <<EOF
spawn /usr/bin/vncpasswd $myuser
expect "Password:"
send "$mypass\r"
expect "Verify:"
send "$mypass\r"
expect eof
exit
EOF
VNC免密-实例二
# 实际测试有效
#!/bin/sh
rpm -ivh xxx.rpm


# 免密部分
###################################################################
myuser="root"
mypasswd="kylin123"
mkdir -p /home/$myuser/.vnc
echo $mypasswd | vncpasswd -f > /home/$myuser/.vnc/passwd
chown -R $myuser:$myuser /home/$myuser/.vnc
chmod 0600 /home/$myuser/.vnc/passwd
###################################################################

# 配置部分,
###################################################################
cp /usr/lib/systemd/system/vncserver@.service /usr/lib/systemd/system/vncserver@:1.service

# 将/usr/lib/systemd/system/vncserver@:1.service中
# 图形编辑:pluma /usr/lib/systemd/system/vncserver@:1.service
	ExecStart=/usr/bin/vncserver_wrapper <USER> %i  改成
	ExecStart=/usr/bin/vncserver_wrapper root %i
	
	PIDFile=/home/<USER>/.vnc/%H%i.pid
	PIDFile=/home/root/.vnc/%H%i.pid
###################################################################



# 手动设置密码,当前什么用户,就会为什么用户设置密码
[服务未授权][root@localhost ~]# vncpasswd 
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used


# 启动服务
	systemctl enable vncserver@:1.service --now
	
# 关闭防火墙
	systemctl disable firewalld --now
	
# 如果有防火墙要求,可以设置防火墙放行规则
	firewall-cmd --permanent --zone=public --add-service=vnc-server
	或者,5801-http,5901,6001-x
	firewall-cmd --permanent --add-port=5901/tcp --permanent
	
# iptables设置
	iptables -t filter -A INPUT -p tcp --dport 5901 -j DROP
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐