centos7.4 安裝配置vnc
[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 <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.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 解開
centos7.4 安裝配置vnc