如果没有安装X-Windows 桌面的话要先安装Xwindows


[root@test-bench system]# yum check-update

[root@test-bench system]# yum groupinstall "X Window System"


启动桌面——startx

#startx      \\ 启动xwindow 进入图形化桌面


设置默认运行级别为图形化


[root@test-bench Desktop]# systemctl get-default          \\查看默认运行级别

multi-user.target

[root@test-bench Desktop]# cat /etc/inittab

# inittab is no longer used when using systemd.

#

# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

#

# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target

#

# systemd uses 'targets' instead of runlevels. By default, there are two main targets:

#

# multi-user.target: analogous to runlevel 3                \\运行级别3:多用户界面

# graphical.target: analogous to runlevel 5                  \\运行级别5:图形化界面

# # To set a default target, run: # # ln -sf /lib/systemd/system/.target 


/etc/systemd/system/default.target #


[root@test-bench system]# systemctl set-default graphical.target    \\设置默认图形化运行级别

rm '/etc/systemd/system/default.target'

ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'



[root@test-bench Desktop]# systemctl get-default            \\查看默认运行级别

graphical.target                                                                     \\图形化设置OK



1,安装VNC packages:

[root@test-bench system]# yum install tigervnc-server -y


2,修改配置信息,在/etc/systemd/system/下建立文件夹vncserver@:1.service 把example config 文件从/lib/systemd/system/[email protected]复制到里面

[root@test-bench system]# cp /lib/systemd/system/[email protected]  /etc/systemd/system/[email protected]


然后打开这个配置文件/etc/systemd/system/[email protected]替换掉默认用户名,找到这一行

ExecStart=/sbin/runuser -l -c "/usr/bin/vncserver %i"

PIDFile=/home//.vnc/%H%i.pid


如果是用root用户登录,就替换成

ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"

PIDFile=/root/.vnc/%H%i.pid


如果是其他用户的话比如john替换如下

ExecStart=/sbin/runuser -l snp01 -c "/usr/bin/vncserver %i"

PIDFile=/home/snp01/.vnc/%H%i.pid


3,重加载 systemd

[root@test-bench system]# systemctl daemon-reload


4,为VNC设密码

[root@test-bench system]# vncpasswd 或

[snp01@test-bench system]# vncpasswd


5,由于我的是Centos 7.4 是用iptable防火墙的所以

vim /etc/sysconfig/iptables

在合适位置加上

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5903 -j ACCEPT

重启iptable


service iptables restart

如果是用Centos 7 默认防火墙的可能需要

[root@test-bench ~]# firewall-cmd --permanent --add-service vnc-server

[root@test-bench ~]# systemctl restart firewalld.service


第六步,设默认启动并开启VNC

[root@test-bench ~]# systemctl enable [email protected]

[root@test-bench ~]# systemctl start  [email protected]


或者link 文件

ln -s /etc/systemd/system/[email protected]  /etc/systemd/system/multi-user.target.wants/vncserver-snp01@:11.service



ln -s /etc/systemd/system/[email protected]  /etc/systemd/system/multi-user.target.wants/vncserver-hru@:12.service


ln -s /etc/systemd/system/[email protected]  /etc/systemd/system/multi-user.target.wants/vncserver-mta@:13.service



vnc 启动报错:


 vncserver-snp01@:11.service - Remote desktop service (VNC)

   Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)

   Active: failed (Result: exit-code) since Sat 2018-06-02 23:32:26 CST; 25s ago

  Process: 8384 ExecStart=/usr/sbin/runuser -l snp01 -c /usr/bin/vncserver %i (code=exited, status=1/FAILURE)

  Process: 8377 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)


Jun 02 23:32:26 test-bench systemd[1]: Starting Remote desktop service (VNC)...

Jun 02 23:32:26 test-bench systemd[1]: vncserver-snp01@:11.service: control process exited, code=exited status=1

Jun 02 23:32:26 test-bench systemd[1]: Failed to start Remote desktop service (VNC).

Jun 02 23:32:26 test-bench systemd[1]: Unit vncserver-snp01@:11.service entered failed state.

Jun 02 23:32:26 test-bench systemd[1]: vncserver-snp01@:11.service failed.


除了检查/tmp/.X11-unix  .ICE-unix 等目录是否有用户文件存在外,还要检查用户是否被设置成nologin了


如果设置成了nologin状态就usermod -s /bin/bash  snp01 解开