作者:admin,发布日期:2019-08-29
阅读:3537;评论:0
写在开头
CentOS 7 默认最小安装完成后,是不带图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方便我们使用,这篇文章我们将详细讲解图形界面的安装和VNC远程的配置。
操作流程
提权到使用root用户
一般我们使用Linux系统时,会使用普通用户去进行一些低权限的操作,但是安装软件涉及到修改系统,所以我们需要提权到root用户。
(这里注意没有普通用户的要记得创建普通用户,因为在root用户下有些图形界面软件是无法运行的(例如Chrome),而且安全性也会大大降低)
[centos@BT ~]$ sudo -i [root@BT ~]#
列出所有的yum包组
我们需要得知图形界面包组的具体名称,以便于安装,这里可以看到图像界面包组的名称是“Server with GUI”
(这一步如果显示是中文的同学请先把语言切换到English,方法网上可以找到)
[root@BT ~]# yum -y grouplist Loaded plugins: fastestmirror, langpacks There is no installed groups file. Maybe run: yum groups mark convert (see man yum) Loading mirror speeds from cached hostfile Available Environment Groups: Minimal Install Compute Node Infrastructure Server File and Print Server Basic Web Server Virtualization Host Server with GUI GNOME Desktop KDE Plasma Workspaces Development and Creative Workstation Available Groups: Compatibility Libraries Console Internet Tools Development Tools Graphical Administration Tools Legacy UNIX Compatibility Scientific Support Security Tools Smart Card Support System Administration Tools System Management Done
安装图形界面
[root@BT ~]# yum -y groupinstall "Server with GUI"
为了防止与服务器连接断开,可以使用tmux 或者screen创建一个窗口来运行
[root@BT ~]# tmux
安装需要一段时间,请耐心等待
设置默认运行级别
设置开机默认为图形界面(这个设置完成之后重启我们显示器就能看到图形界面了)
ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target reboot
设置完成,重启系统。
VNC的安装
我们要远程连接服务器,就会用到VNC,VNC相当于windows下的远程桌面,可以帮助你连接到服务器的图形界面。
[root@BT ~]# yum -y install tigervnc-server Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package tigervnc-server.x86_64 0:1.8.0-13.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================== Installing: tigervnc-server x86_64 1.8.0-13.el7 base 215 k Transaction Summary ======================================================================================================================================== Install 1 Package Total download size: 215 k Installed size: 509 k Downloading packages: tigervnc-server-1.8.0-13.el7.x86_64.rpm | 215 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : tigervnc-server-1.8.0-13.el7.x86_64 1/1 Verifying : tigervnc-server-1.8.0-13.el7.x86_64 1/1 Installed: tigervnc-server.x86_64 0:1.8.0-13.el7 Complete!
开启测试VNC服务器
这里需要使用普通用户操作
[root@BT ~]# exit # 退出root用户 logout [centos@BT ~]$ vncserver :1 # 开启VNC服务器(:1为端口5901,以此类推) You will require a password to access your desktops. Password: (这里需要输入密码,密码是看不到的) Verify: (确认密码) Would you like to enter a view-only password (y/n)? n A view-only password is not used New 'BT:1 (centos)' desktop is BT:1 Creating default startup script /home/centos/.vnc/xstartup Creating default config /home/centos/.vnc/config Starting applications specified in /home/centos/.vnc/xstartup Log file is /home/centos/.vnc/BT:1.log
开启后查看端口
[centos@BT ~]$ ss -ant State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 5 *:5901 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:6001 *:* LISTEN 0 5 192.168.122.1:53 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* ESTAB 0 208 10.0.0.4:22 39.188.45.114:2279 LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:6010 :::* LISTEN 0 5 :::5901 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::6001 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::*
可以发现5901已经监听
连接VNC服务器
这里需要用到VNC viewer,下载地址:
https://www.realvnc.com/en/connect/download/viewer/
输入IP:端口,连接服务器,然后输入密码,这里需要点Continue(测试用服务器已经关闭了,所以请不要尝试攻击)
连接成功如图
设置VNC为服务并开机启动
继续切换到ROOT用户,进行下面的操作,创建服务文件
[centos@BT ~]$ sudo -i [root@BT ~]# cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:1.service
[Unit] Description=Remote desktop service (VNC) After=syslog.target network.target [Service] Type=forking # Clean any existing files in /tmp/.X11-unix environment ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i" PIDFile=/home/<USER>/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target
这里请把<USER>替换成普通用户的用户名(我这里是centos)
接着保存,设置开机启动并且开启服务(--now参数为启动服务)
systemctl enable --now vncserver@\:1.service
检查服务状态,显示已在运行
[root@BT ~]# systemctl status vncserver@\:1.service ● vncserver@:1.service - Remote desktop service (VNC) Loaded: loaded (/usr/lib/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2019-08-29 12:38:16 CST; 45s ago Process: 4166 ExecStart=/usr/sbin/runuser -l centos -c /usr/bin/vncserver %i (code=exited, status=0/SUCCESS) Process: 4163 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 2011 (Xvnc) CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service ‣ 2011 /usr/bin/Xvnc :1 -auth /home/centos/.Xauthority -desktop BT:1 (centos) -fp catalogue:/etc/X11/fontpath.d -geometry ... Aug 29 12:38:12 BT systemd[1]: Starting Remote desktop service (VNC)... Aug 29 12:38:16 BT systemd[1]: Started Remote desktop service (VNC).
至此,安装完成