Páginas

domingo, 3 de octubre de 2021

FreeBSD vm-bhyve X11 Forwarding usando SSH

Ejecutar aplicaciones X11 desde invitado vm-bhyve en host.

La(s) aplicacion(es) se ejecutan en la máquina virtual invitada, sin embargo, se muestra en el host. La importancia de esto radica en que puede ejecutar una aplicación en una lugar y mostrar la ventana en el host.

Si una aplicación no es soportada en la máquina host pero bien soportada en la vm podrá ejecutarse en una ventana del host.

Ejecutar Firefox en una vm y mostrarlo en el host FreeBSD 13.0
basado en el reenvío X11 usando ssh.

# vm list
NAME          DATASTORE  LOADER     CPU  MEMORY  VNC  AUTOSTART  STATE
alpine        default    grub       1    512M    -    No         Stopped
arch          default    grub       1    512M    -    No         Stopped
freebsd-12-2  default    bhyveload  1    256M    -    No         Stopped

Iniciar y conectarse a la máquina virtual para permitir el reenvío X11.

# vm start alpine
# vm console alpine

# vm switch info

------------------------
Virtual Switch: public
------------------------
  type: standard
  ident: vm-public
  vlan: -
  physical-ports: re0
  bytes-in: 673338 (657.556K)
  bytes-out: 602940 (588.808K)

  virtual-port
    device: tap2
    vm: alpine

# vm info

------------------------
Virtual Machine: alpine
------------------------
  state: running (25889)
  datastore: default
  loader: grub
  uuid: 3dfa8278-1ca9-11ec-b451-fc3fdbd15275
  uefi: default
  cpu: 1
  memory: 512M
  memory-resident: 123006976 (117.308M)

  console-ports
    com1: /dev/nmdm-alpine.1B

  network-interface
    number: 0
    emulation: virtio-net
    virtual-switch: public
    fixed-mac-address: 58:9c:fc:03:37:ea
    fixed-device: -
    active-device: tap2
    desc: vmnet-alpine-0-public
    mtu: 1500
    bridge: vm-public
    bytes-in: 291480 (284.648K)
    bytes-out: 2158 (2.107K)

  virtual-disk
    number: 0
    device-type: file
    emulation: virtio-blk
    options: -
    system-path: /zroot/vm/alpine/disk0.img
    bytes-size: 10737418240 (10.000G)
    bytes-used: 1160348672 (1.080G)

  snapshots
    zroot/vm/alpine@snap1_300921	452K	jue. sept. 30 17:31 2021

------------------------
Virtual Machine: arch
------------------------
  state: running (43467)
  datastore: default
  loader: grub
  uuid: a923082e-1deb-11ec-9f91-fc3fdbd15275
  uefi: default
  cpu: 1
  memory: 512M
  memory-resident: 164139008 (156.535M)

  console-ports
    com1: /dev/nmdm-arch.1B

  network-interface
    number: 0
    emulation: virtio-net
    virtual-switch: public
    fixed-mac-address: 58:9c:fc:0e:36:e4
    fixed-device: -
    active-device: tap0
    desc: vmnet-arch-0-public
    mtu: 1500
    bridge: vm-public
    bytes-in: 15132 (14.777K)
    bytes-out: 0 (0.000B)

  virtual-disk
    number: 0
    device-type: file
    emulation: virtio-blk
    options: -
    system-path: /zroot/vm/arch/disk0.img
    bytes-size: 9663676416 (9.000G)
    bytes-used: 4176012288 (3.889G)

  snapshots
    zroot/vm/arch@snap1_300921	2.55M	jue. sept. 30 17:31 2021

------------------------
Virtual Machine: freebsd-12-2
------------------------
  state: running (52279)
  datastore: default
  loader: bhyveload
  uuid: e7d636e0-236b-11ec-8cb5-fc3fdbd15275
  uefi: default
  cpu: 1
  memory: 256M
  memory-resident: 138010624 (131.617M)

  console-ports
    com1: /dev/nmdm-freebsd-12-2.1B

  network-interface
    number: 0
    emulation: virtio-net
    virtual-switch: public
    fixed-mac-address: 58:9c:fc:0b:f1:00
    fixed-device: -
    active-device: tap1
    desc: vmnet-freebsd-12-2-0-public
    mtu: 1500
    bridge: vm-public
    bytes-in: 300815 (293.764K)
    bytes-out: 1947 (1.901K)

  virtual-disk
    number: 0
    device-type: file
    emulation: virtio-blk
    options: -
    system-path: /zroot/vm/freebsd-12-2/disk0.img
    bytes-size: 9663676416 (9.000G)
    bytes-used: 2805482496 (2.612G)

# /etc/hosts

# Host Database
#
# This file should contain the addresses and aliases for local hosts that
# share this file.  Replace 'my.domain' below with the domainname of your
# machine.
#
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
#
#
::1                     localhost localhost.my.domain
127.0.0.1               localhost localhost.my.domain
192.168.88.183          fbsd
192.168.88.149          arch
192.168.88.150          alpine
192.168.88.151          freebsd-12-2
#
# Imaginary network.
#10.0.0.2               myname.my.domain myname

carlos@freebsd:~ % ssh carlos@alpine

The authenticity of host 'alpine (192.168.88.150)' can't be established.
ECDSA key fingerprint is SHA256:SwvwUhTBiIMr4IdzZ4yvaIcu21Xf4WSF2B5+tABLHz0.
No matching host key fingerprint found in DNS.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'alpine' (ECDSA) to the list of known hosts.
carlos@alpine's password:
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

alpine:~$ su
Password:
alpine:/home/carlos# 

alpine:/home/carlos#  apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
v3.14.2-68-gbf3cc5c973 [http://dl-cdn.alpinelinux.org/alpine/v3.14/main]
OK: 4791 distinct packages available

alpine:/home/carlos#  apk add vim
OK: 1008 MiB in 146 packages

Cambiar en el archivo /etc/ssh/sshd_config

alpine:/home/carlos# vim /etc/ssh/sshd_config

X11Forwarding no
por
X11Forwarding yes

Guardar y salir

alpine:/home/carlos# vim /etc/apk/repositories 

#/media/cdrom/apks
#http://dl-cdn.alpinelinux.org/alpine/v3.14/main
#http://dl-cdn.alpinelinux.org/alpine/v3.14/community
http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/community
http://dl-cdn.alpinelinux.org/alpine/edge/testing

alpine:/home/carlos# apk update
fetch http://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/testing/x86_64/APKINDEX.tar.gz
v3.15.0_alpha20210804-3464-gdb0fea5e0a [http://dl-cdn.alpinelinux.org/alpine/edge/main]
v3.15.0_alpha20210804-3463-g250f668bc0 [http://dl-cdn.alpinelinux.org/alpine/edge/community]
v3.15.0_alpha20210804-3457-g949ce4d971 [http://dl-cdn.alpinelinux.org/alpine/edge/testing]
OK: 20227 distinct packages available

alpine:/home/carlos# reboot

alpine:/home/carlos# Connection to alpine closed by remote host.
Connection to alpine closed.
carlos@freebsd:~ %

carlos@freebsd:~ % ssh carlos@alpine
carlos@alpine's password:
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

alpine:~$ 

En la máquina host, (freebsd), utilizando xhost se habilita sólo al usuario autorizado.

carlos@freebsd:~ % xhost
access control enabled, only authorized clients can connect

Se añade la dirección IP de Alpine:

carlos@freebsd:~ % xhost +inet:192.168.88.150
192.168.88.150 being added to access control list
carlos@freebsd:~ %

Ejecutar la aplicación Firefox
dentro de la máquina virtual, pero sólo se muestra en el host:

Se ejecuta usando ssh:

carlos@freebsd:~ % ssh carlos@alpine -X "firefox no-remote"
carlos@alpine's password:
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

/usr/bin/xauth:  file /home/carlos/.Xauthority does not exist
alpine:~$

Para no introducir más contraseñas se generan las claves pública y privada nuevamente:

carlos@freebsd:~ % ssh-keygen -t rsa -b 4096 -C "carlos@freebsd"
Generating public/private rsa key pair.
Enter file in which to save the key (/usr/home/carlos/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /usr/home/carlos/.ssh/id_rsa.
Your public key has been saved in /usr/home/carlos/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RkURbfj//UwqDmVOhVOQQ1qMae69/DZueCzFliRdHZc carlos@freebsd
The key's randomart image is:
+---[RSA 4096]----+
|         .=X+=.o+|
|         .=oO oEo|
|        .o.= =   |
|       .  o B    |
|        S. O o   |
|       .  o B .  |
|           Oo. .o|
|          o.Oo +o|
|           +.oo +|
+----[SHA256]-----+

Arch Linux instalar openssh
# vim /etc/pacman.conf

[multilib]
Include = /etc/pacman.d/mirrorlist

Guardar y salir

# pacman -Syu

# pacman -S openssh

[root@arch ~]# systemctl status sshd
* sshd.service - OpenSSH Daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor pre>
     Active: inactive (dead)
     
[root@arch ~]# systemctl start sshd
[root@arch ~]# ls -a
.   .bash_history  .config  .lesshst  Desktop	 Downloads  Pictures  Templates
..  .cache	   .gnupg   .viminfo  Documents  Music	    Public    Videos

[root@arch ~]# systemctl status sshd
* sshd.service - OpenSSH Daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor pre>
     Active: active (running) since Mon 2021-10-04 19:03:03 CEST; 37s ago

Iniciar el servicio sshd con el arranque del sistema:

[root@arch ~]# systemctl enable sshd

Copiar a las vm

carlos@freebsd:~ % ssh-add
Identity added: /usr/home/carlos/.ssh/id_rsa (carlos@freebsd)
carlos@freebsd:~ % ssh-copy-id carlos@alpine
carlos@alpine's password: 
carlos@freebsd:~ % 

El procedimiento es el mismo para las demás máquinas virtuales

carlos@freebsd:~ % ssh-copy-id carlos@arch
carlos@alpine's password: 
carlos@freebsd:~ %      
           
carlos@freebsd:~ % ssh-copy-id carlos@freebsd-12-2
carlos@alpine's password: 
carlos@freebsd:~ %                           
                  
Llegados a este punto la conexión ssh se realiza mediante clave.

carlos@freebsd:~ % ssh carlos@alpine -X "firefox no-remote" &
[1] 96751
carlos@freebsd:~ %

carlos@freebsd:~ % su
Password:

root@freebsd:/usr/home/carlos # vm list
NAME          DATASTORE  LOADER     CPU  MEMORY  VNC  AUTOSTART  STATE
alpine        default    grub       1    512M    -    No         Running (40797)
arch          default    grub       1    512M    -    No         Stopped
freebsd-12-2  default    bhyveload  1    256M    -    No         Stopped
root@freebsd:/usr/home/carlos # vm stop alpine
Sending ACPI shutdown to alpine
root@freebsd:/usr/home/carlos # vm list
NAME          DATASTORE  LOADER     CPU  MEMORY  VNC  AUTOSTART  STATE
alpine        default    grub       1    512M    -    No         Stopped
arch          default    grub       1    512M    -    No         Stopped
freebsd-12-2  default    bhyveload  1    256M    -    No         Stopped
root@freebsd:/usr/home/carlos # 

FreeBSD es genial!.

No hay comentarios:

Publicar un comentario