140

I'm using an amazon Ubuntu EC2 instance which is only has a command line interface. I want to setup UI for that server to access using remote desktop tools. Is there any way to apply GUI to the EC2 instance?

bjb568
  • 9,826
  • 11
  • 45
  • 66
Elisha
  • 1,588
  • 3
  • 9
  • 9

5 Answers5

203

This can be done. Following are the steps to setup the GUI

Create new user with password login

sudo useradd -m awsgui
sudo passwd awsgui
sudo usermod -aG admin awsgui

sudo vim /etc/ssh/sshd_config # edit line "PasswordAuthentication" to yes

sudo /etc/init.d/ssh restart

Setting up ui based ubuntu machine on AWS.

In security group open port 5901. Then ssh to the server instance. Run following commands to install ui and vnc server:

sudo apt-get update
sudo apt-get install ubuntu-desktop
sudo apt-get install vnc4server

Then run following commands and enter the login password for vnc connection:

su - awsgui

vncserver

vncserver -kill :1

vim /home/awsgui/.vnc/xstartup

Then hit the Insert key, scroll around the text file with the keyboard arrows, and delete the pound (#) sign from the beginning of the two lines under the line that says "Uncomment the following two lines for normal desktop." And on the second line add "sh" so the line reads

exec sh /etc/X11/xinit/xinitrc. 

When you're done, hit Ctrl + C on the keyboard, type :wq and hit Enter.

Then start vnc server again.

vncserver

You can download xtightvncviewer to view desktop(for Ubutnu) from here https://help.ubuntu.com/community/VNC/Clients

In the vnc client, give public DNS plus ":1" (e.g. www.example.com:1). Enter the vnc login password. Make sure to use a normal connection. Don't use the key files.

Additional guide available here: http://www.serverwatch.com/server-tutorials/setting-up-vnc-on-ubuntu-in-the-amazon-ec2-Page-3.html

Mac VNC client can be downloaded from here: https://www.realvnc.com/en/connect/download/viewer/

Port opening on console

sudo iptables -A INPUT -p tcp --dport 5901 -j ACCEPT

If the grey window issue comes. Mostly because of ".vnc/xstartup" file on different user. So run the vnc server also on same user instead of "awsgui" user.

vncserver

sugunan
  • 4,242
  • 6
  • 34
  • 61
  • 13
    I think you're missing `su - awsgui` after `sudo usermod -aG admin awsgui` – Konstantin K Dec 11 '14 at 10:38
  • 14
    Remember to open port 5901 in your Security Group for this to work. Thanks for the detailed answer! – Daniel Magliola Jan 20 '15 at 11:46
  • 12
    Tried a couple of guides, including this one, and I only get a grey background - no ubuntu desktop. – Wrench Feb 11 '15 at 23:10
  • 5
    I did this which solved the grey background for me https://www.digitalocean.com/community/questions/vnc-gray-screen-ubuntu-14-04 – timhc22 Feb 23 '15 at 00:57
  • 1
    the command of "sudo usermod -aG admin awsgui" returns an error message of "usermod: group 'admin' does not exist" when executed the command under $ec2-user – d4v1dv00 Mar 31 '15 at 01:42
  • hello @sugunan, I tried the solution you have stated here. How do I connect from a vnc client? in the public dns, I gave the amazon ec2 dns, as stated at the instance page, but I get the following error. "Connection to host ec2-54-205-75-105.us-east-1c.compute.amazonaws.com::5901 was closed." Can you please state an example with a vnc client? It is really urgent and your help is much appreciated. – Lakshmi Narayanan Sep 01 '15 at 14:10
  • 8
    Try `vim .vnc/xstartup` if `vim awsgui/.vnc/xstartup` didn't work – TomasVeras Oct 29 '15 at 00:12
  • 2
    You can also just do this as the regular ubuntu user, no need to create a new awsgui user – JHowIX Oct 29 '15 at 00:20
  • 2
    I got a grey screen myself. I installed gnome (sudo apt-get install ubuntu-gnome-desktop) and used the third configuration file to fix it (Ubuntu Trusty Tahr 14.04LTS) on this page http://www.havetheknowhow.com/Configure-the-server/Install-VNC.html – user1097111 Mar 08 '16 at 23:39
  • @d4v1dv00 You can create the "admin" group by typing `addgroup admin` – mareoraft Mar 19 '16 at 02:01
  • 1
    Instead of exposing ports like vnc, consider ssh tunnel into the host. – Mike D May 13 '16 at 12:03
  • @TomasVeras Or `vim /home/users/awsgui/.vnc/xstartup`. – Raffi Khatchadourian May 23 '16 at 18:27
  • @DanielMagliola love your comment about the Security Group; for other noobs: you have to do this in the security-group-settings in the AWS management console! Check which security group your machine is configured in and then add a port rule for that security group. – dakami May 15 '17 at 16:10
  • You shouldn't decrease your machine security more than it is necessary. You don't have to enable password login and give access to 5901 port. You also don't need third party vnc viewer on mac os - built-in screen sharing works out of the box. Good solution (which doesn't have a problem with grey background) can be found here: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-16-04 – Maciej Kozieł Dec 14 '17 at 18:44
  • 2
    If you get a grey background only, just follow the additional steps by yuchien below. – Dylan Hogg Dec 15 '17 at 09:25
  • i used the vnc client tightvnc viewer in windows.If you want to start vncserver with different port then use the command vncserver -rfbport – ramnar Dec 27 '18 at 15:52
  • 1
    I'm getting "The connection was refused by the computer" whenever I try to connect using Real VNC Viewer on Mac. Please help someone – gapc311 Sep 09 '19 at 17:59
  • Which AMI of aws will be preferable? – Abhay Jan 06 '20 at 18:28
79

So I follow first answer, but my vnc viewer gives me grey screen when I connect to it. And I found this Ask Ubuntu link to solve that.

The only difference with previous answer is you need to install these extra packages:

apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

And use this ~/.vnc/xstartup file:

#!/bin/sh

export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &

gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &

Everything else is the same.

Tested on EC2 Ubuntu 14.04 LTS.

Community
  • 1
  • 1
yuchien
  • 1,382
  • 1
  • 10
  • 14
  • 2
    This worked for me on top of the previous answer and with `su - awsgui` done before running the vnc commands. – Vincenzo Pii Apr 15 '16 at 12:44
  • 1
    This step was necessary to get it working after following most tutorials about how to setup ubuntu desktop on aws with tightvncserver. None of the tutorials worked for me without this step. – techdog Jul 15 '16 at 02:37
  • 1
    You may need to reboot your OS after following these steps.. I followed this answer and has to reboot first. – tno2007 Nov 24 '16 at 11:26
  • I tried the above steps and I can see the Ubuntu on RealVNC. But I can see only the terminal and desktop. Somehow other UI parts like Toolbar, Applications etc are missing. Any other steps are there or any fix for this? – Vinayak Jan 22 '17 at 10:47
  • Run the following in terminal: `killall gnome-panel && sudo gnome-panel &` – Octocat Apr 29 '17 at 18:12
  • had to `chmod 755 ~/.vnc/xstartup` for me for the grey screen to go away. Tested on ubuntu16.04 LTS – VarsMolta May 21 '17 at 14:12
  • I did everything correctly. The first couple of times it worked but then the gnome-panel disappeared and blank desktop background is showing. I tried multiple times it has been the same. Any solution? – Sourav Oct 12 '19 at 03:45
17

For Ubuntu 16.04

1) Install packages

$ sudo apt update;sudo apt install --no-install-recommends ubuntu-desktop
$ sudo apt install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal vnc4server

2) Edit /usr/bin/vncserver file and modify as below

Find this line

"# exec /etc/X11/xinit/xinitrc\n\n".

And add these lines below.

"gnome-session &\n".
"gnome-panel &\n".
"gnome-settings-daemon &\n".
"metacity &\n".
"nautilus &\n".
"gnome-terminal &\n".

3) Create VNC password and vnc session for the user using "vncserver" command.

lonely@ubuntu:~$ vncserver
You will require a password to access your desktops.
Password:
Verify:
xauth: file /home/lonely/.Xauthority does not exist
New 'ubuntu:1 (lonely)' desktop is ubuntu:1
Creating default startup script /home/lonely/.vnc/xstartup
Starting applications specified in /home/lonely/.vnc/xstartup
Log file is /home/lonely/.vnc/ubuntu:1.log

Now you can access GUI using IP/Domain and port 1

stackoverflow.com:1

Tested on AWS and digital ocean .

For AWS, you have to allow port 5901 on firewall

To kill session

$ vncserver -kill :1

Refer:

https://linode.com/docs/applications/remote-desktop/install-vnc-on-ubuntu-16-04/

Refer this guide to create permanent sessions as service

http://www.krizna.com/ubuntu/enable-remote-desktop-ubuntu-16-04-vnc/

Quake231
  • 171
  • 1
  • 2
  • It worked, but after rebooting the machine, ssh stopped connecting with connection timout error. Do you have any idea why its happening? – Shan Khan Oct 02 '18 at 10:26
9

1) Launch Ubuntu Instance on EC2.
2) Open SSH Port in instance security.
3) Do SSH to instance.
4) Execute:

sudo apt-get update    sudo apt-get upgrade

5) Because you will be connecting from Windows Remote Desktop, edit the sshd_config file on your Linux instance to allow password authentication.

sudo vim /etc/ssh/sshd_config

6) Change PasswordAuthentication to yes from no, then save and exit.
7) Restart the SSH daemon to make this change take effect.

sudo /etc/init.d/ssh restart

8) Temporarily gain root privileges and change the password for the ubuntu user to a complex password to enhance security. Press the Enter key after typing the command passwd ubuntu, and you will be prompted to enter the new password twice.

sudo –i
passwd ubuntu

9) Switch back to the ubuntu user account and cd to the ubuntu home directory.

su ubuntu
cd

10) Install Ubuntu desktop functionality on your Linux instance, the last command can take up to 15 minutes to complete.

export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get update
sudo -E apt-get install -y ubuntu-desktop

11) Install xrdp

sudo apt-get install xfce4
sudo apt-get install xfce4 xfce4-goodies

12) Make xfce4 the default window manager for RDP connections.

echo xfce4-session > ~/.xsession

13) Copy .xsession to the /etc/skel folder so that xfce4 is set as the default window manager for any new user accounts that are created.

sudo cp /home/ubuntu/.xsession /etc/skel

14) Open the xrdp.ini file to allow changing of the host port you will connect to.

sudo vim /etc/xrdp/xrdp.ini

(xrdp is not installed till now. First Install the xrdp with sudo apt-get install xrdp then edit the above mentioned file)

15) Look for the section [xrdp1] and change the following text (then save and exit [:wq]).

port=-1
- to -
port=ask-1

16) Restart xrdp.

sudo service xrdp restart

17) On Windows, open the Remote Desktop Connection client, paste the fully qualified name of your Amazon EC2 instance for the Computer, and then click Connect.

18) When prompted to Login to xrdp, ensure that the sesman-Xvnc module is selected, and enter the username ubuntu with the new password that you created in step 8. When you start a session, the port number is -1.

19) When the system connects, several status messages are displayed on the Connection Log screen. Pay close attention to these status messages and make note of the VNC port number displayed. If you want to return to a session later, specify this number in the port field of the xrdp login dialog box.

See more details: https://aws.amazon.com/premiumsupport/knowledge-center/connect-to-linux-desktop-from-windows/
http://c-nergy.be/blog/?p=5305

WELZ
  • 240
  • 2
  • 7
  • 18
akshay hundia
  • 189
  • 3
  • 10
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bhargav Rao Feb 15 '16 at 11:42
  • 3
    I am not able to find [xrdp1] section in xrdp.ini file. – Sudhir Dec 17 '18 at 06:27
3

For LXDE/Lubuntu


1. connect to your instance (local forwarding port 5901)

ssh -L 5901:localhost:5901 -i "xxx.pem" ubuntu@xxx.amazonaws.com

2. Install packages

sudo apt update && sudo apt upgrade
sudo apt-get install xorg lxde vnc4server lubuntu-desktop

3. Create /etc/lightdm/lightdm.conf

sudo nano /etc/lightdm/lightdm.conf

4. Copy and paste the following into the lightdm.conf and save

[SeatDefaults]
allow-guest=false
user-session=LXDE
#user-session=Lubuntu

5. setup vncserver (you will be asked to create a password for the vncserver)

vncserver
sudo echo "lxpanel & /usr/bin/lxsession -s LXDE &" >> ~/.vnc/xstartup

6. Restart your instance and reconnect

sudo reboot
ssh -L 5901:localhost:5901 -i "xxx.pem" ubuntu@xxx.amazonaws.com

7. Start vncserver

vncserver -geometry 1280x800

8. In your Remote Desktop Client (e.g. Remmina) set Server to localhost:5901 and protocol to VNC

Arash Joorabchi
  • 339
  • 2
  • 6