Enable remote desktop for Gnome from command line?
If I understand you right: you want to share gnome or other environment remotely as it is, then the easiest way to achieve this is to use x11vnc. It shares real X11 server as it is after user logged in:
x11vnc -display :0
Or if you want vnc server run after login, you can automate with this script:
#!/bin/bash
/usr/bin/x11vnc -nap -wait 50 -noxdamage -passwd PASSWORD -display :0 -forever -o /var/log/x11vnc.log -bg
You can place this script in startup programs in gnome, so that it could be run automatically when the user logins. Please note that this script is not secure as session PASSWORD variable is clearly seen to anyone who could read the file and anyone knowing password can connect to vnc session (password in this case is 8 symbols word asked when you are connecting remotely). If you want more secure connection search how to do vnc ssh tunneling.
My favorite method for remote connections is to use vino. It's similar to x11vnc, but I find it much easier to set up (though I'm typically using a GUI). With Vino enabled, gnome is set up to accept vnc connections for the active session (the one that is currently logged in), for every boot. Any windows or applications open on the screen will be viewable in the vnc connection.
In normal cases (e.g., through a GUI), it's enough to set it up by running
$ vino-preferences
In the absence of a GUI, the settings must be changed using gsettings
. Something like
$ gsettings set org.gnome.Vino enabled true
$ gsettings set org.gnome.Vino view-only true
$ gsettings set org.gnome.Vino authentication-methods "['vnc']"
$ gsettings set org.gnome.Vino prompt-enabled false
$ gsettings set org.gnome.Vino require-encryption true
would enable remote desktop with sane values. You can see the full list of options as well as a description of their effects by opening dconf-editor
and navigating to desktop.gnome.remote-access
.
If your computer has multiple users, Vino will need to be set up for each user.
To connect to your remote session, you can use any standard vnc client. However, you must forward port 5900 to the computer you want to connect to from your router's firmware. Alternatively, if you are also allowing for ssh connections to these computers, it may be easier and more secure to use vnc through an ssh tunnel. From your local machine:
ssh -L 5900:localhost:5900 <remote server>
Then open up a vnc client and connect to 127.0.0.1:5900 and log in with your remote server's username and password.
I was able to set a fresh Ubuntu 16.04 install from a remote ssh connection with the following script:
#!/bin/bash
export DISPLAY=:0
read -e -p "VNC Password: " -i "ubuntu" password
dconf write /org/gnome/desktop/remote-access/enabled true
dconf write /org/gnome/desktop/remote-access/prompt-enabled false
dconf write /org/gnome/desktop/remote-access/authentication-methods "['vnc']"
dconf write /org/gnome/desktop/remote-access/require-encryption false
dconf write /org/gnome/desktop/remote-access/vnc-password \"\'$(echo -n $password | base64)\'\"
dconf dump /org/gnome/desktop/remote-access/
sudo service lightdm restart
The quoting is important for any of the string settings (single ticks inside quotes).
For dconf
to be able to write it needs access to XWindows, so that's why the export DISPLAY
part is needed. I think you still need to be logged in to the desktop on the actual Ubuntu machine to connect with VNC after this.
The dump command is just there to confirm all the settings took hold, you don't really need that.