2018年2月22日 星期四

[Linux 文章收集] How to Install and Configure VNC on Ubuntu 16.04

Source From Here 
Introduction 
VNC, or "Virtual Network Computing", is a connection system that allows you to use your keyboard and mouse to interact with a graphical desktop environment on a remote server. It makes managing files, software, and settings on a remote server easier for users who are not yet comfortable with the command line. 

In this guide, we will be setting up VNC on an Ubuntu 16.04 server and connecting to it securely through an SSH tunnel. The VNC server we will be using is TightVNC, a fast and lightweight remote control package. This choice will ensure that our VNC connection will be smooth and stable even on slower internet connections. 

Prerequisites 
To complete this tutorial, you'll need: 
* An Ubuntu 16.04 Droplet set up via the Ubuntu 16.04 initial server setup tutorial, which includes having a sudo non-root user. Note that this tutorial can be completed using any size Droplet, but a VNC built on a smaller droplet may have more limits on functionality than a larger one.
* A local computer with a VNC client installed that supports VNC connections over SSH tunnels. If you are using Windows, you could use TightVNC, RealVNC, or UltraVNC. Mac OS X users can use the built-in Screen Sharing program, or can use a cross-platform app like RealVNC. Linux users can choose from many options: vinagre, krdc, RealVNC, TightVNC, and more.


Step 1 — Installing the Desktop Environment and VNC Server 
By default, an Ubuntu 16.04 Droplet does not come with a graphical desktop environment or a VNC server installed, so we'll begin by installing those. Specifically, we will install packages for the latest Xfce desktop environment and the TightVNC package available in the official Ubuntu repository. 

On your server, install the Xfce and TightVNC packages. 
$ sudo apt-get update
$ sudo apt install xfce4 xfce4-goodies tightvncserver

To complete the VNC server's initial configuration after installation, use the vncserver command to set up a secure password. 
$ vncserver

You'll be prompted to enter and verify a password, and also a view-only password. Users who log in with the view-only password will not be able to control the VNC instance with their mouse or keyboard. This is a helpful option if you want to demonstrate something to other people using your VNC server, but isn't necessary (See also vncpasswd - change the VNC password). 

Running vncserver completes the installation of VNC by creating default configuration files and connection information for our server to use. With these packages installed, you are now ready to configure your VNC server. 

Step 2 — Configuring the VNC Server 
First, we need to tell our VNC server what commands to perform when it starts up. These commands are located in a configuration file called xstartup in the .vnc folder under your home directory. The startup script was created when you ran the vncserver in the previous step, but we need modify some of the commands for the Xfce desktop. 

When VNC is first set up, it launches a default server instance on port 5901. This port is called a display port, and is referred to by VNC as :1. VNC can launch multiple instances on other display ports, like :2, :3, etc. When working with VNC servers, remember that :X is a display port that refers to 5900+X. Because we are going to be changing how the VNC server is configured, we'll need to first stop the VNC server instance that is running on port 5901. 
$ vncserver -kill :1
Killing Xtightvnc process ID 17648

Before we begin configuring the new xstartup file, let's back up the original: 
$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now create a new xstartup file with nano or your favorite text editor. 
$ vi ~/.vnc/xstartup
  1. #!/bin/bash  
  2. xrdb $HOME/.Xresources  
  3. startxfce4 &  

The first command in the file, xrdb $HOME/.Xresources, tells VNC's GUI framework to read the server user's .Xresources file. .Xresources is where a user can make changes to certain settings of the graphical desktop, like terminal colors, cursor themes, and font rendering. The second command simply tells the server to launch Xfce, which is where you will find all of the graphical software that you need to comfortably manage your server. To ensure that the VNC server will be able to use this new startup file properly, we'll need to grant executable privileges to it and start vncserver again. 
$ sudo chmod +x ~/.vnc/xstartup
$ vncserver // Start vncserver
New 'X' desktop is your_server_name.com:1

Starting applications specified in /home/sammy/.vnc/xstartup
Log file is /home/sammy/.vnc/liniverse.com:1.log

Step 3 — Testing the VNC Desktop 
In this step, we'll test the connectivity of your VNC server. 

First, we need to create an SSH connection on your local computer that securely forwards to the localhost connection for VNC. You can do this via the terminal on Linux or OS X with following command. Remember to replace user and server_ip_address with the sudo non-root username and IP address of your server. 
// -L local_socket:host:hostport: Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side.
// -N: Do not execute a remote command. This is useful for just forwarding ports.
// -l login_name: Specifies the user to log in as on the remote machine.
$ ssh -L 5901:127.0.0.1:5901 -N -f -l username server_ip_address

If you are using a graphical SSH client, like PuTTY, use server_ip_address as the connection IP, and set localhost:5901 as a new forwarded port in the program's SSH tunnel settings. Next, you may now use a VNC client to attempt a connection to the VNC server at localhost:5901. You'll be prompted to authenticate. The correct password to use is the one you set in Step 1. 

Once you are connected, you should see the default Xfce desktop. It should look something like this: 
 

Step 4 — Creating a VNC Service File 
Next, we'll set up the VNC server as a systemd service. This will make it possible to start, stop, and restart it as needed, like any other systemd service. First, create a new unit file called /etc/systemd/system/vncserver@.service using your favorite text editor: 
$ sudo vi /etc/systemd/system/vncserver@.service
  1. [Unit]  
  2. Description=Start TightVNC server at startup  
  3. After=syslog.target network.target  
  4.   
  5. [Service]  
  6. Type=forking  
  7. User=  
  8. PAMName=login  
  9. PIDFile=/home//.vnc/%H:%i.pid  
  10. ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1  
  11. ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i  
  12. ExecStop=/usr/bin/vncserver -kill :%i  
  13.   
  14. [Install]  
  15. WantedBy=multi-user.target  

$ sudo systemctl daemon-reload // make the system aware of the new unit file.
$ sudo systemctl enable vncserver@1.service // Enable the unit file.

Be sure to change the value of and the username in the value of PIDFILE to match your username. The 1 following the @ sign signifies which display number the service should appear over, in this case the default :1 as was discussed above. Now let's stop the current instance of the VNC server if it's still running: 
$ vncserver -kill :1

Then start it as you would start any other systemd service: 
$ sudo systemctl start vncserver@1 // Start the service
$ sudo systemctl status vncserver@1 // Check the status of service
● vncserver@1.service - Start TightVNC server at startup
Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-02-22 06:14:54 PST; 1s ago
...

Conclusion 
You should now have a secured VNC server up and running on your Ubuntu 16.04 server. Now you'll be able to manage your files, software, and settings with an easy-to-use and familiar graphical interface. 

Supplement 
Disable / Turn Off Firewall in Ubuntu Linux Server 
浅析 Linux 初始化 init 系统,第 3 部分 (systemd) 
Systemd 是 Linux 系統中最新的初始化系統(init),它主要的設計目標是克服 sysvinit 固有的缺點,提高系統的啟動速度。systemd 和 ubuntu 的 upstart 是競爭對手,預計會取代 UpStart,實際上在作者寫作本文時,已經有消息稱 Ubuntu 也將採用 systemd 作為其標準的系統初始化系統...

CREATING AND MODIFYING SYSTEMD UNIT FILES

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...