你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu 16.10安装Xfce桌面与VNC远程连接

[日期:2017-07-11] 来源:Linux公社  作者:Linux [字体: ]

在远程服务器上运行桌面

通常在远程Linux服务器上工作时,您可以使用ssh终端。 但是,有时您需要在服务器上运行GUI应用程序,并保持运行一段时间。

最近我不得不做类似的事情,所以我设置一个Ubuntu服务器与桌面,并通过VNC访问它。

这个想法实现很简单。 在服务器上安装您选择的任何桌面环境。 在本教程中,我们将使用Xfce,因为它与Gnome和KDE相比更具小巧。

然后使用vnc服务器启动桌面环境,并创建一个X显示会话,我们将通过vnc客户端从本地台式机访问。

安装桌面环境和VNC服务器

Xfce是一款轻量级桌面,非常适合在远程服务器上使用。 首先安装xfce软件包和tightvnc服务器。 在进行实际安装之前,最好先更新包缓存。

sudo apt-get update
sudo apt-get install xfce4 xfce4-goodies tightvncserver

请注意,这将只安装包,而不是启动任何东西。 我们将在本指南后面自行启动具体设置的vncserver。

如果dpkg进程意外退出,则可能必须运行以下命令 -
#sudo dpkg --configure -a

为vnc创建一个新用户

接下来要做的是创建一个将在vnc会话期间使用的unix用户。 用户名可以是任何东西。 使用adduser命令。

# adduser mike
Adding user `mike' ...
Adding new group `mike' (1001) ...
Adding new user `mike' (1001) with group `mike' ...
Creating home directory `/home/mike' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for mike
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]
#

vncserver将使用此unix用户启动桌面环境。 这意味着,在远程桌面上工作时,您将成为此用户

为用户设置“vnc密码”

vnc服务器维护一个单独的密码,用于通过vnc客户端登录到vnc服务器。 该密码与unix用户密码不同。 它使用vncpasswd命令配置。

首先切换到上一步中创建的用户“mike”,并设置vnc服务器密码。

su - mike

接下来使用vncpasswd命令

$ vncpasswd
Using password file /home/mike/.vnc/passwd
VNC directory /home/mike/.vnc does not exist, creating.
Password:
Verify: 
Would you like to enter a view-only password (y/n)?
mike@bunty:~$

请注意,passwd文件不存在,并在此步骤中首次创建。

如果您以前已经运行vncserver命令,那么它将创建文件。 当您第一次运行vncserver时,它会创建一个默认启动脚本

$ vncserver

You will require a password to access your desktops.

Password:                                                                                                                                           
Password too short
enlightened@desktop:~$ vncserver

You will require a password to access your desktops.

Password:
Verify: 
Would you like to enter a view-only password (y/n)? n

New 'X' desktop is desktop:1

Creating default startup script /home/enlightened/.vnc/xstartup
Starting applications specified in /home/enlightened/.vnc/xstartup
Log file is /home/enlightened/.vnc/desktop:1.log

但是,我们不需要运行vncserver命令。 它将使用启动脚本自动启动。

创建xstartup脚本

下一个重要的文件是xstartup脚本。 它包含有关哪些X应用程序开始的说明。 桌面环境是我们必须开始的X应用程序。

如果文件已经存在,首先要备份该文件
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

现在用nano编辑它

vnc@server:~$ nano .vnc/xstartup

注 - 这是用户vnc的主目录,即/home/mike/.vnc/xstartup

在xstartup脚本中输入以下几行

#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

startxfce4命令将启动xfce桌面。 保存文件并关闭它。

使xstartup文件可执行。 这是必要的,以便vncserver可以执行此文件。

$ chmod +x ~/.vnc/xstartup

创建vnc服务文件

下一步是创建vnc服务文件,以便我们可以使用service命令启动vnc服务器,而不必每次都运行vncserver命令。

确保在USER变量中输入正确的用户名。 这是vnc服务器将用于启动桌面会话的用户。

root@linuxidc:~# sudo nano /etc/init.d/vncserver

粘贴以下脚本

#!/bin/bash
PATH="$PATH:/usr/bin/"
export USER="mike"
DISPLAY="1"
DEPTH="16"
GEOMETRY="1024x768"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions

case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;

stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;

restart)
$0 stop
$0 start
;;
esac
exit 0

保存文件并关闭它。 使其可执行

# chmod +x /etc/init.d/vncserver

开始服务

开始并测试我们的步骤。

首先重新加载systemctl,以便它可以使用vncserver启动脚本。
systemctl守护进程重新加载

现在启动vncserver。 它在端口5901上启动服务器
#service vncserver start

检查它的运行

root@linuxidc:~# service vncserver status
● vncserver.service
  Loaded: loaded (/etc/init.d/vncserver; bad; vendor preset: enabled)
  Active: active (exited) since Thu 2017-03-02 05:36:42 UTC; 6s ago
    Docs: man:systemd-sysv-generator(8)
  Process: 24877 ExecStart=/etc/init.d/vncserver start (code=exited, status=0/SUCCESS)

Mar 02 05:36:40 linuxidc systemd[1]: Starting vncserver.service...
Mar 02 05:36:40 linuxidc vncserver[24877]:  * Starting vncserver for user 'vnc' on localhost:1...
Mar 02 05:36:40 linuxidc su[24885]: Successful su for vnc by root
Mar 02 05:36:40 linuxidc su[24885]: + ??? root:vnc
Mar 02 05:36:40 linuxidc su[24885]: pam_unix(su:session): session opened for user vnc by (uid=0)
Mar 02 05:36:42 linuxidc vncserver[24877]: New 'X' desktop is linuxidc:1
Mar 02 05:36:42 linuxidc vncserver[24877]: Starting applications specified in /home/vnc/.vnc/xstartup
Mar 02 05:36:42 linuxidc vncserver[24877]: Log file is /home/vnc/.vnc/linuxidc:1.log
Mar 02 05:36:42 linuxidc systemd[1]: Started vncserver.service.$ cat ~/.vnc/*.pid
18577
18731# ps -ef | grep tightvnc
vnc      24574    1  0 05:32 ?        00:00:00 Xtightvnc :1 -desktop X -auth /home/vnc/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth /home/vnc/.vnc/passwd -rfbport 5901 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
root    24744 10412  0 05:33 pts/0    00:00:00 grep --color=auto tightvnc
root@linuxidc:~#

检查vnc服务器的打开端口。 从vnc客户端连接时,需要正确的端口号

# netstat -nlp | grep vnc
tcp        0      0 0.0.0.0:5901            0.0.0.0:*              LISTEN      24574/Xtightvnc
tcp        0      0 0.0.0.0:6001            0.0.0.0:*              LISTEN      24574/Xtightvnc
unix  2      [ ACC ]    STREAM    LISTENING    5225386  24574/Xtightvnc    /tmp/.X11-unix/X1

Vnc server can also be started by calling the script directly.

也可以通过直接调用脚本来启动Vnc服务器。

# /etc/init.d/vncserver start
[ ok ] Starting vncserver (via systemctl): vncserver.service.
root@linuxidc:~#

停止vncserver

# service vncserver stop

在桌面上安装vncviewer客户端

现在,我们将vnc服务器启动并运行GUI桌面环境。

在Ubuntu上安装xtightvncviewer。

$ sudo apt-get install xtightvncviewer

现在使用vncviewer命令连接到远程vnc服务器。

$ vncviewer -quality 5 -encodings“copyrect tight hextile zlib corre rre raw”-compresslevel 5 IPADDR:5901

我们使用较低质量和压缩编码来压缩正在传输的图像数据并使其更快。

使用像KRDC这样的其他vnc查看器可能会更慢。

 

CentOS7.1安装VNC,让Win7远程桌面Linux  http://www.linuxidc.com/Linux/2017-05/143346.htm

CentOS 7 安装配置 VNC 详解  http://www.linuxidc.com/Linux/2017-05/143324.htm

Ubuntu下安装配置VNC远程工具  http://www.linuxidc.com/Linux/2017-03/141936.htm

更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-07/145552.htm

linux
相关资讯       VNC远程连接 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款