X11 Forwarding for non-root user not working - vagrant

Cross-Posted
Environmental Details
Relevant bits of server's /etc/ssh/sshd_config:
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
Relevant bits of client's $HOME/.ssh/config:
Host *
XAuthLocation /opt/X11/bin/xauth
ForwardX11 yes
ForwardX11Trusted yes
Using XQuartz on macOS High Sierra.
The Problem
I'm spinning up a vagrant Ubuntu 18.04 VM. I've added a second user outside of the vagrant user.
ssh -X vagrant#ubuntu-bionic xclock
I can get X11Forwarding to work when I login as the vagrant user. I cannot get X11Forwarding to work when I login as the ops user.
ssh -X ops#ubuntu-bionic xclock
X11 forwarding request failed on channel 0
Error: Can't open display:
I want to be able to get it to work with the ops user. From the client, $DISPLAY has a value. When I login with vagrant, $DISPLAY has a value. When I login as ops, $DISPLAY is unset. If I set $DISPLAY to match what it is with the vagrant user, I get the same error:
Error: Can't open display: localhost:10.0
when X11UseLocalhost yes
Error: Can't open display: ubuntu-bionic:10.0
when X11UseLocalhost no
If I login as ops and then sudo su - vagrant, $DISPLAY remains unset. If I login as vagrant and then sudo su - ops, $DISPLAY is inherited.
What am I missing to get this to work? I've run xhost + in each user (including sudo -s root xhost +) and it still doesn't work.
If I add -vv to my ssh commands, I see this message when connecting as vagrant:
X11 forwarding request accepted on channel 0
and as ops:
Remote: X11 forwarding disabled in user configuration file.
X11 forwarding request failed on channel 0

I don't get the whole topic, but for my very similar situation it helped to create the users~/.Xauthorityfile. I copied it from the vagrant users home directory and then set the new ownership.

On macOS BigSur with XQuartz 2.8.1 (xorg-server 1.20.11):
Setting the following into my Vagrantfile solved it for me (for ubuntu 20.04):
config.vm.provision "shell",
inline: "apt-get update && apt-get upgrade -y && apt-get install xauth -y"
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
The whole Vagrantfile would be:
Vagrant.require_version ">= 2.2.3"
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |v, override|
override.vm.box = "bento/ubuntu-20.04"
end
config.vm.provision "shell",
inline: "apt-get update && apt-get upgrade -y && apt-get install xauth firefox firefox-geckodriver -y"
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
end
Allows running firefox -no-remote https://stackoverflow.com/ through the X server.
The -no-remote flag is added since it seems to reduce the latency.
As inspired by a guide on How to enable and use SSH X11 Forwarding on Vagrant Instances by Josphat Mutai.

Related

Sharing ubuntu 16.04 vm folder with mac os (with samba)

I followed million tutorials and guides on how to do it but no luck... Something is missing, hope you guys can help me out.
My OS: macOS Sierra
Virtualbox OS: Ubuntu 16.04
VM Network Adapter: Bridged Adapter
These are my steps:
Connect to vm via ssh
Install samba
$ sudo apt install samba
Create a directory to be shared
$ sudo mkdir /media/testshare
Add new share to smb.conf
$ sudo vim /etc/samba/smb.conf
[testshare]
comment = My Shared Folder
path = /media/testshare
browseable = yes
readonly = no
guest ok = yes
Restart samba
Add a samba password
$ sudo smbpasswd -a {username}
Try to connect from mac: In Finder, Command+K to open "Connect to Server" and in Server Address: smb://192.168.0.104
I'm getting this:
I tried smb://192.168.0.104/testshare, smb://192.168.0.104/media/testshare
I have no idea how to make it work. Help!
EDIT:
Might help, this is what I get when I run
$ sudo netstat -tulpn | egrep "samba|smbd|nmbd|winbind":
* I know that the ip is different now (192.168.0.104 => 192.168.0.109), its a new vm, don't pay attention to that.
Host OS: macOS El Capitan
VM (guest) OS: Ubuntu Server 16.04.5 LTS
VirtualBox v5.2.18
Both host OS and guest OS must have same user (further in the text: username).
Stage 1: Install VirtualBox Guest Additions:
1.1. Locate the VirtualBox Guest Additions,
$ cd /Applications/VirtualBox.app/Contents/MacOS/
$ cp VBoxGuestAdditions.iso ~/Downloads/
1.2. Start the VM
1.3. Click the CD icon in the bottom right task bar
1.4. Select "Choose disk image..."" and search for the VBoxGuestAdditions.iso
1.5. In the guest terminal type (you can also do this from the host terminal if you SSH into it):
$ sudo su
$ apt update
$ apt upgrade
$ apt-get install dkms build-essential linux-headers-generic gcc make
$ mount /dev/cdrom /mnt
$ cd /mnt
$ sh ./VBoxLinuxAdditions.run
$ reboot
Stage 2: Shared Folders Setup:
2.1. Create rules in VM:
Stop the VM
Go to Settings > Shared Folders
Click in the Add new port forwarding rule green button in the top right of the window.
Search and select the folder you would like to share (e.g.: /path/to/shared/host_folder)
Select the Auto-mount and Make Permanent options
Start the VM
2.2. To mount shared folder on /opt you must create shared_folder_dir subfolder and set appropriate permissions to it:
$ sudo mkdir -p /opt/shared_folder_dir
$ sudo chmod ug+w -Rv /opt/shared_folder_dir
$ sudo chown username:username -Rv /opt/shared_folder_dir
2.3. Add username to the vboxsf group:
$ sudo adduser username vboxsf
$ sudo usermod -a -G vboxsf username
2.4. Reboot VM to apply changes:
$ sudo reboot
Stage 3: Auto mounting host_folder into /opt/shared_folder_dir:
3.1. Change VM's /etc/rc.local:
$ sudo nano /etc/rc.local
and place following right above exit 0:
# 'folder_name' = given in the shared folders configuration
# 'path/to/shared/folders' = guest path to access the shared folders from
# 'id' = prints uid/gid
# sudo mount -t vboxsf -o uid={uid},gid={gid} {shared_folder_name} {path/to/shared/folder}
sleep 5
sudo mount -t vboxsf -o uid=1000,gid=1000 host_folder /opt/shared_folder_dir
exit 0
<<< EOF >>>
Note:
I've added sleep 5 to execute mount operation after VirtualBox Guest Additions has started. You can check that by journalctl -b command.
3.2. Reboot VM to apply changes:
$ sudo reboot
See also

Why does Jupyter require an unknown password when hosted on a vagrant VM?

I am trying to set up a vagrant box with python and use Jupyter to run code from my browser on the host. Here is a minimal example:
Vagrantfile:
Vagrant.configure('2') do |config|
config.vm.box = 'ubuntu/trusty64'
config.vm.network :forwarded_port, guest: 8888, host: 8888
config.vm.provision "shell", inline: <<-SHELL
apt-get install -y python-dev python-pip
pip install jupyter
SHELL
end
In my windows host cmd I execute these commands in the same directory as the Vagrantfile:
> vagrant up
> vagrant ssh
~$ jupyter notebook --no-browser --ip=0.0.0.0
I then navigate to localhost:8888 in Chrome on my windows host only to find a Jupyter login page asking for a password I have never set. I have tried entering no password and "vagrant" in case it was linked to the VM's password.
Why is Jupyter asking me for a password?
There is also a way to completely disable jupyter authentication by setting NotebookApp.token to empty string in jupyter_notebook_config.py or when launching server with something like:
jupyter notebook --no-browser --ip=0.0.0.0 --NotebookApp.token=''
This inherently not secure and may be acceptable only for development purposes on your local environment.
This comment covers a recent change to Jupyter.
If you do:
jupyter notebook list
This will give you the token that you will need to then pass in:
http://localhost:8888/?token=yourToken
The token will also be outputted to the console when you start the notebook for the first time:
vagrant#vagrant-ubuntu-trusty-64:~$ jupyter notebook --no-browser --ip=0.0.0.0
Serving notebooks from local directory: /home/vagrant
0 active kernels
The Jupyter Notebook is running at: http://0.0.0.0:8888/?token=yourToken

Reload VM in Vagrant script

Introduction
I'm using Vagrant and want to create a box that fits my needs. I'm currently building my provisioning script but I have a problem which would require me to relog into the box.
What I'm trying to achieve
I want to set my locales to the German language
What I'm doing
After logging into my vagrant box with vagrant ssh I'm running the following commands
sudo apt-get update
sudo cp /var/www/projectfantasy/www/vagrant_ressources/locale.gen /etc/
sudo locale-gen de_DE.UTF-8
These steps are done with the help of Debian's wiki. The last step is
To use the new settings with your programs, log out and back in.
And now I am where I need help. How would I relog while being in the vagrant provisioning script? When I don't relog I'm getting the following warnings when installing extra packages.
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "de_DE.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
I mean, these are only warnings so that shouldn't be a problem, right? But I don't want to have warnings and would like to know how I could fix this issue.
When I relog and try to install those packages again it works without any problems.
unfortunately, this is not so simple - if you issue a reboot command or shutdown -r now in your provisioning script the VM will restart (indeed) but then the provisioning will not continue.
fortunately, some people wrote plugin (https://github.com/exratione/vagrant-provision-reboot and https://github.com/aidanns/vagrant-reload) I experienced with the second and it works
make sure to install the plugin
$ vagrant plugin install vagrant-reload
in your vagrantfile, your provisioning will look like
config.vm.provision "shell", path: "vagrant_ressources/preparations.sh"
config.vm.provision :reload
config.vm.provision "shell", path: "vagrant_ressources/bootstrap.sh"
so preparations.sh will execute, then the VM will reload and bootstrap.sh will execute
in your preparations.sh, after you generate locale, make sure variables are set in the locale file :
sudo locale-gen
echo -e 'LANG=de_DE.UTF-8\nLC_ALL=de_DE.UTF-8' > /etc/default/locale
sudo timedatectl set-timezone Europe/Berlin
PS : one note on your usage of the shell provisioning. By default provisioning is run with root privilege so there is no need for all the sudo ; if you want to run the provisioning as vagrant user run it as config.vm.provision :shell, privileged: false ... and you will need the sudo
I couldn't reproduce your warning, but I find this will allow you to set LC_ALL:
sudo update-locale LC_ALL=de_DE.UTF-8
(from https://askubuntu.com/questions/114759/warning-setlocale-lc-all-cannot-change-locale, but works for debian jessie as well)

vagrant box copy not working well

I cannot boot a vagrant box copy without problem.
I use "hashicorp/precise32" (I cannot install "ubuntu/trusty64" or 32 on my machine, I've tried everything. I guess it's because my pc is too old, 2009).
I can vagrant up using the default added box "hashicorp/precise32".
All works fine.
Then, I vagrant halt and do a copy of the vagrant box like that :
vagrant package --base myprojectvm_default_1437507203066_25705 --output mybox.box
vagrant box add mybox.box --name mybox
Then I vagrant destroy.
Then I change Vagrantfile : config.vm.box = "hashicorp/precise32"
to config.vm.box = "mybox"
THen I run vagrant up and I see this in my guest after minutes :
"Waiting for network configuration...
Waiting up to 60 more seconds for network configuration....
Booting system without full network configuration..."
Then some "Starting..." lines among other lines... and finally, machine booted.
Why "without full network configuration" ?
From the vagrant book
It is very common for Linux-based boxes to fail to boot initially.
This is often a very confusing experience because it is unclear why it
is happening. The most common case is because there are persistent
network device udev rules in place that need to be reset for the new
virtual machine. To avoid this issue, remove all the persistent-net
rules. On Ubuntu, these are the steps necessary to do this:
$ rm /etc/udev/rules.d/70-persistent-net.rules
$ mkdir /etc/udev/rules.d/70-persistent-net.rules
$ rm -rf /dev/.udev/
$ rm /lib/udev/rules.d/75-persistent-net-generator.rules>
on centos box, this is the command I am running
cd /etc/sysconfig/network-scripts/
sudo rm -f ifcfg-eth1
cd /etc/udev/rules.d/
sudo rm -f 70-persistent-net.rules
It then has always been working fine for me

Understanding vagrant with a docker provisioner

So my goal here is to have Vagrant spin me up a VM that I can use as a rails web application development environment. I am very, very new to Vagrant, Docker, etc. and wanted to check if this even makes sense:
Should I be creating one Docker image, or two? My thought was that one image would be for rvm, ruby, and rails, and the other image would be for Apache.
The goal would be something that could be distributed to a few people, who would then get the exact same environment set up easily.
Here is my current Vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "phusion/ubuntu-12.04-amd64"
config.vm.network "forwarded_port", :guest => 80, :host => 5000
# Share the docker configs
config.vm.synced_folder "./docker", "/docker"
config.vm.provision "docker" do |d|
d.build_image "-t me/rvm-dev /docker/rvm-dev"
d.run "me/rvm-dev"
end
config.vm.host_name = "coursera-rail.dev"
config.vm.network :forwarded_port, guest:80, host:8080
end
And here is my current (probably wrong) Dockerfile
(as an aside, I am trying to find some good guidance on creating a "docker" user account so I don't run everything as root)
## Dockerfile
FROM ubuntu:latest
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y curl
RUN mkdir -p /tmp/downloads
## Install RVM, Ruby, Rails
RUN cd /tmp/downloads
RUN \curl -L https://get.rvm.io | bash -s stable --ruby --rails
Lastly, the above works, and if I use vagrant ssh I can head in to my VM, but obviously I am unable to use RVM without doing docker run -i -t me/rvm-dev and getting a shell with docker. Is this supposed to be the workflow?
In my humble opinion, the best flow would be to
1) separate your app and db stack into vagrant boxes
or
2) run docker on the host (the physical machine) and your DB instance (mysql, etc..) inside a vagrant box.
Option one is best because you are "sandboxing" both layers of the stack (DB and APP) inside Vagrant thus leaving the OS on your local machine (windows, Linux, Mac OSX) instact. Of course, you could install/run your IDE on the physical machine.

Resources