Howto vncserver » Historie » Version 1
Jeremias Keihsler, 13.01.2017 10:18
1 | 1 | Jeremias Keihsler | h1. VNCServer |
---|---|---|---|
2 | |||
3 | h2. preliminary note |
||
4 | |||
5 | this information is taken from [[https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-remote-access-for-the-gnome-desktop-on-centos-7]] |
||
6 | |||
7 | <pre><code class="bash"> |
||
8 | yum groupinstall "GNOME Desktop" |
||
9 | reboot |
||
10 | </code></pre> |
||
11 | |||
12 | <pre><code class="bash"> |
||
13 | yum install tigervnc-server |
||
14 | </code></pre> |
||
15 | |||
16 | h2. ad hoc VNC Service |
||
17 | |||
18 | you most likely used to access a server occasionally. |
||
19 | First we need to set the VNC password. These are not the users' Linux passwords, but the users' password to log in to the VNC sessions. |
||
20 | |||
21 | Execute the following command: |
||
22 | |||
23 | <pre><code class="bash"> |
||
24 | vncpasswd |
||
25 | </code></pre> |
||
26 | |||
27 | to start and stop the VNC-Server you can do: |
||
28 | <pre><code class="bash"> |
||
29 | vncserver :1 -geometry 1400x1000 -depth 24 |
||
30 | </code></pre> |
||
31 | |||
32 | <pre><code class="bash"> |
||
33 | vncserver -kill :1 |
||
34 | </code></pre> |
||
35 | |||
36 | If you run the VNC-Service only as long as necessary noone can tamper around with it. |
||
37 | |||
38 | In this case you maybe don't want to open your firewall as well, you can use a SSH-tunnel to access the VNC-Server |
||
39 | |||
40 | <pre><code class="bash"> |
||
41 | ssh -L 6000:localhost:5901 user@example.com -N |
||
42 | </code></pre> |
||
43 | |||
44 | h2. VNC Service for multiple Clients |
||
45 | |||
46 | h3. create multiple test user |
||
47 | |||
48 | First, we will create two user accounts. These accounts will remotely connect to our CentOS 7 server from VNC clients. |
||
49 | * joevnc |
||
50 | * janevnc |
||
51 | |||
52 | Run the following command to add a user account for joevnc: |
||
53 | |||
54 | <pre><code class="bash"> |
||
55 | useradd -c "User Joe Configured for VNC Access" joevnc |
||
56 | </code></pre> |
||
57 | |||
58 | Then run the passwd command to change joevnc's password: |
||
59 | |||
60 | <pre><code class="bash"> |
||
61 | passwd joevnc |
||
62 | </code></pre> |
||
63 | |||
64 | The output will ask us for new password. Once supplied, the account will be ready for login: |
||
65 | |||
66 | <pre><code class="bash"> |
||
67 | Changing password for user joevnc. |
||
68 | New password: |
||
69 | Retype new password: |
||
70 | passwd: all authentication tokens updated successfully. |
||
71 | </code></pre> |
||
72 | |||
73 | Next, create an account for janevnc: |
||
74 | |||
75 | <pre><code class="bash"> |
||
76 | useradd -c "User Jane Configured for VNC Access" janevnc |
||
77 | passwd janevnc |
||
78 | </code></pre> |
||
79 | |||
80 | h3. Setting VNC Passwords |
||
81 | |||
82 | In this step, the users will need to set their VNC passwords. These are not the users' Linux passwords, but the passwords to log in to the VNC sessions. |
||
83 | |||
84 | Open another terminal connection to the CentOS 7 server, and this time log in as joevnc. |
||
85 | |||
86 | <pre><code class="bash"> |
||
87 | ssh joevnc@your_server_ip |
||
88 | </code></pre> |
||
89 | |||
90 | Execute the following command: |
||
91 | |||
92 | <pre><code class="bash"> |
||
93 | vncpasswd |
||
94 | </code></pre> |
||
95 | |||
96 | h3. Set-up VNC Service |
||
97 | |||
98 | VNC server doesn't start automatically when it's first installed. To check this, run the following command: |
||
99 | |||
100 | <pre><code class="bash"> |
||
101 | systemctl status vncserver@:.service |
||
102 | </code></pre> |
||
103 | |||
104 | The output will be like this: |
||
105 | <pre><code class="bash"> |
||
106 | vncserver@:.service - Remote desktop service (VNC) |
||
107 | Loaded: loaded (/usr/lib/systemd/system/vncserver@.service; disabled) |
||
108 | Active: inactive (dead) |
||
109 | </code></pre> |
||
110 | |||
111 | You can also run this command: |
||
112 | |||
113 | <pre><code class="bash"> |
||
114 | systemctl is-enabled vncserver@.service |
||
115 | </code></pre> |
||
116 | |||
117 | This should show output like this: |
||
118 | |||
119 | <pre><code class="bash"> |
||
120 | disabled |
||
121 | </code></pre> |
||
122 | |||
123 | So why is it disabled? That's because each user will start a separate instance of the VNC service daemon. In other words, VNC doesn't run as one single process that serves every user request. Each user connecting via VNC will have to start a new instance of the daemon (or the system administrator can automate this). |
||
124 | |||
125 | CentOS 7 uses the systemd daemon to initiate other services. Each service that natively runs under systemd has a service unit file that's placed under the @/lib/systemd/system@ directory by the yum installer. Processes that get started automatically at boot time have a link to this service unit file placed in the @/etc/systemd/system/@ directory. |
||
126 | |||
127 | In our case, a generic service unit file was created in the @/lib/systemd/system/@ directory, but no link was made under @/etc/systemd/system/@. To test this, run the following commands: |
||
128 | |||
129 | <pre><code class="bash"> |
||
130 | ls -l /lib/systemd/system/vnc* |
||
131 | </code></pre> |
||
132 | |||
133 | You should see: |
||
134 | |||
135 | <pre><code class="bash"> |
||
136 | -rw-r--r--. 1 root root 1744 Jun 10 16:15 /lib/systemd/system/vncserver@.service |
||
137 | </code></pre> |
||
138 | |||
139 | Then check under @/etc/systemd/system/@: |
||
140 | |||
141 | <pre><code class="bash"> |
||
142 | ls -l /etc/systemd/system/*.wants/vnc* |
||
143 | </code></pre> |
||
144 | |||
145 | Thos one doesn't exist: |
||
146 | |||
147 | <pre><code class="bash"> |
||
148 | ls: cannot access /etc/systemd/system/*.wants/vnc*: No such file or directory |
||
149 | </code></pre> |
||
150 | |||
151 | So, the first step is to start two new instances of VNC server for our two users. To do this, we will need to make two copies of the generic VNC service unit file under @/etc/system/system@. In the code snippet below, you're making two copies with two different names: |
||
152 | |||
153 | <pre><code class="bash"> |
||
154 | cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:4.service |
||
155 | cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:5.service |
||
156 | </code></pre> |
||
157 | |||
158 | So why did we add two numbers (along with the colon) in the copied file names? |
||
159 | |||
160 | Again, that comes back to the concept of individual VNC services. VNC by itself runs on port 5900. Since each user will run their own VNC server, each user will have to connect via a separate port. The addition of a number in the file name tells VNC to run that service as a sub-port of 5900. So in our case, joevnc's VNC service will run on port 5904 (5900 + 4) and janevnc's will run on 5905 (5900 + 5). |
||
161 | |||
162 | Next edit the service unit file for each client. Open the @/etc/systemd/system/vncserver@:4.service@ file with the vim editor: |
||
163 | |||
164 | <pre><code class="bash"> |
||
165 | vim /etc/systemd/system/vncserver@:4.service |
||
166 | </code></pre> |
||
167 | |||
168 | A look at the "Quick HowTo" section tells us we have already completed the first step. Now we need to go through the remaining steps. The comments also tell us that VNC is a non-trusted connection. We will talk about this later. |
||
169 | |||
170 | For now, edit the @[Service]@ section of the file, replacing instances of @<USER>@ with @joevnc@. Also, add the @-geometry 1280x1024@ clause at the end of the @ExecStart@ parameter. This just tells VNC the screen size it should start in. You will modify two lines in total. Here's what the edited file should look like (note that the entire file is not shown): |
||
171 | |||
172 | <pre> |
||
173 | # The vncserver service unit file |
||
174 | # |
||
175 | # Quick HowTo: |
||
176 | # 1. Copy this file to /etc/systemd/system/vncserver@:<display>.service |
||
177 | # 2. Edit <USER> and vncserver parameters appropriately |
||
178 | # ("runuser -l <USER> -c /usr/bin/vncserver %i -arg1 -arg2") |
||
179 | # 3. Run `systemctl daemon-reload` |
||
180 | # 4. Run `systemctl enable vncserver@:<display>.service` |
||
181 | # |
||
182 | |||
183 | . . . |
||
184 | |||
185 | [Unit] |
||
186 | Description=Remote desktop service (VNC) |
||
187 | After=syslog.target network.target |
||
188 | |||
189 | [Service] |
||
190 | Type=forking |
||
191 | # Clean any existing files in /tmp/.X11-unix environment |
||
192 | ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' |
||
193 | ExecStart=/sbin/runuser -l joevnc -c "/usr/bin/vncserver %i -geometry 1280x1024" |
||
194 | PIDFile=/home/joevnc/.vnc/%H%i.pid |
||
195 | ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' |
||
196 | |||
197 | [Install] |
||
198 | WantedBy=multi-user.target |
||
199 | </pre> |
||
200 | |||
201 | Save the file and exit vi. |
||
202 | |||
203 | Similarly, open the @/etc/systemd/system/vncserver@:5.service@ file in vim and make the changes for user janevnc: |
||
204 | |||
205 | <pre><code class="bash"> |
||
206 | vim /etc/systemd/system/vncserver@:5.service |
||
207 | </code></pre> |
||
208 | |||
209 | Here's just the [Service] section with the changes marked: |
||
210 | |||
211 | <pre> |
||
212 | ... |
||
213 | [Service] |
||
214 | Type=forking |
||
215 | # Clean any existing files in /tmp/.X11-unix environment |
||
216 | ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' |
||
217 | ExecStart=/sbin/runuser -l janevnc -c "/usr/bin/vncserver %i -geometry 1280x1024" |
||
218 | PIDFile=/home/janevnc/.vnc/%H%i.pid |
||
219 | ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' |
||
220 | ... |
||
221 | </pre> |
||
222 | |||
223 | Next, run the following commands to reload the systemd daemon and also to make sure VNC starts up for two users at boot time. |
||
224 | |||
225 | <pre><code class="bash"> |
||
226 | systemctl daemon-reload |
||
227 | </code></pre> |
||
228 | |||
229 | Enable the first server instance: |
||
230 | <pre><code class="bash"> |
||
231 | systemctl enable vncserver@:4.service |
||
232 | </code></pre> |
||
233 | |||
234 | Output: |
||
235 | |||
236 | <pre><code class="bash"> |
||
237 | ln -s '/etc/systemd/system/vncserver@:4.service' '/etc/systemd/system/multi-user.target.wants/vncserver@:4.service' |
||
238 | </code></pre> |
||
239 | |||
240 | Enable the second server instance: |
||
241 | <pre><code class="bash"> |
||
242 | systemctl enable vncserver@:5.service |
||
243 | </code></pre> |
||
244 | |||
245 | h2. Configuring your Firewall |
||
246 | |||
247 | *If you need to access the VNC-Service and don't want to use a SSH-tunnel, then you might consider reconfiguring your Firewall* |
||
248 | |||
249 | Next, we will need to configure the firewall to allow VNC traffic through ports 5904 and 5905 only. CentOS 7 uses Dynamic Firewall through the firewalld daemon; the service doesn't need to restart for changes to take effect. |
||
250 | |||
251 | The firewalld service should start automatically at system boot time, but it's always good to check: |
||
252 | |||
253 | <pre><code class="bash"> |
||
254 | firewall-cmd --state |
||
255 | </code></pre> |
||
256 | |||
257 | This should show: |
||
258 | <pre><code class="bash"> |
||
259 | running |
||
260 | </code></pre> |
||
261 | |||
262 | If the state is "not running" for any reason, execute the following command to make sure it's running: |
||
263 | <pre><code class="bash"> |
||
264 | systemctl start firewalld |
||
265 | </code></pre> |
||
266 | |||
267 | Now add the rules for ports 5904 and 5905: |
||
268 | |||
269 | <pre><code class="bash"> |
||
270 | firewall-cmd --permanent --zone=public --add-port=5904-5905/tcp |
||
271 | firewall-cmd --reload |
||
272 | </code></pre> |