190 lines
6.9 KiB
Markdown
190 lines
6.9 KiB
Markdown
# bash-101-resources
|
|
|
|
Materiales utilizados para el taller de bash celebrado por el ETSIIT Tech Club el miércoles 29 de octubre.
|
|
|
|
Cada equipo tiene un contenedor LXC en Proxmox con una terminal web (ttyd), los retos por niveles y un marcador en vivo. Los scripts cubren lo que se repite por equipo o se ejecuta durante el evento; lo que se hace una sola vez está aquí abajo como notas. El diseño completo está en [PLAN.md](PLAN.md).
|
|
|
|
| Máquina | Dirección |
|
|
|---|---|
|
|
| Sobremesa (`virbr-taller`) | 192.168.150.1 |
|
|
| Host Proxmox (`vmbr0`) | 192.168.150.2 |
|
|
| CT proxy (199) | 192.168.150.10 en `eth0` (`vmbr0`), 10.10.10.1 en `eth1` (`vmbr1`) |
|
|
| CT del equipo n | 10.10.10.(100+n), solo en `vmbr1` |
|
|
|
|
## Montaje
|
|
|
|
### 1. VM en el sobremesa
|
|
|
|
Red de libvirt propia en modo `open`, en la que libvirt no pone reglas de firewall. En la red `default` (NAT), la cadena `guest_input` de libvirt rechaza toda conexión nueva hacia la VM, también las que llegan por DNAT, así que el paso 4 no funcionaría.
|
|
|
|
```sh
|
|
export LIBVIRT_DEFAULT_URI=qemu:///system
|
|
cat > taller-net.xml <<'EOF'
|
|
<network>
|
|
<name>taller</name>
|
|
<forward mode='open'/>
|
|
<bridge name='virbr-taller'/>
|
|
<ip address='192.168.150.1' netmask='255.255.255.0'>
|
|
<dhcp><range start='192.168.150.100' end='192.168.150.199'/></dhcp>
|
|
</ip>
|
|
</network>
|
|
EOF
|
|
virsh net-define taller-net.xml && virsh net-autostart taller && virsh net-start taller
|
|
|
|
virt-install --name pve --osinfo debian13 \
|
|
--vcpus 10 --cpu host-passthrough \
|
|
--memory 16384 --memballoon none \
|
|
--controller type=scsi,model=virtio-scsi \
|
|
--disk size=32,bus=scsi,cache=none,discard=unmap \
|
|
--disk size=40,format=raw,bus=scsi,cache=none,discard=unmap \
|
|
--network network=taller,model=virtio \
|
|
--cdrom proxmox-ve_9.*.iso
|
|
```
|
|
|
|
En el instalador: sistema en `sda`, IP `192.168.150.2/24`, puerta de enlace y DNS `192.168.150.1`. `sdb` se queda vacío para ZFS.
|
|
|
|
### 2. Proxmox
|
|
|
|
```sh
|
|
# Repositorio no-subscription
|
|
rm -f /etc/apt/sources.list.d/{pve-enterprise,ceph}.sources
|
|
cat > /etc/apt/sources.list.d/proxmox.sources <<'EOF'
|
|
Types: deb
|
|
URIs: http://download.proxmox.com/debian/pve
|
|
Suites: trixie
|
|
Components: pve-no-subscription
|
|
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
|
|
EOF
|
|
apt update && apt full-upgrade -y
|
|
apt install -y curl qrencode # template.sh y teams.sh
|
|
|
|
# Pool ZFS en el segundo disco (compruébalo con lsblk)
|
|
zpool create -o ashift=12 -o autotrim=on -O compression=lz4 -O atime=off tank /dev/sdb
|
|
pvesm add zfspool tank --pool tank --content rootdir,images
|
|
|
|
# ARC limitado a 4 GiB, ya y tras reiniciar
|
|
echo "options zfs zfs_arc_max=$((4 << 30))" > /etc/modprobe.d/zfs.conf
|
|
update-initramfs -u -k all
|
|
echo $((4 << 30)) > /sys/module/zfs/parameters/zfs_arc_max
|
|
```
|
|
|
|
### 3. Red interna y firewall
|
|
|
|
```sh
|
|
# Bridge sin puertos ni IP; el segundo comando aplica la configuración
|
|
pvesh create /nodes/"$(hostname)"/network --iface vmbr1 --type bridge --autostart 1
|
|
pvesh set /nodes/"$(hostname)"/network
|
|
|
|
pvesh set /cluster/firewall/options --enable 1
|
|
pvesh set /nodes/"$(hostname)"/firewall/options --enable 1
|
|
```
|
|
|
|
Las reglas por defecto mantienen SSH y la GUI accesibles desde la red local (192.168.150.0/24). Si entras desde otra red, añade tu IP al ipset `management` antes de activar el firewall.
|
|
|
|
### 4. DNAT en el sobremesa
|
|
|
|
Va antes que el proxy porque certbot necesita el puerto 80 accesible desde fuera. El router tiene que reenviar 80 y 443 al sobremesa, y `DOMAIN` (en `common.sh`) tiene que resolver a la IP pública.
|
|
|
|
`taller.nft` (ajusta `WAN` a la interfaz del sobremesa hacia el router):
|
|
|
|
```nft
|
|
define WAN = "enp5s0"
|
|
define PROXY = 192.168.150.10
|
|
|
|
table ip taller
|
|
delete table ip taller
|
|
table ip taller {
|
|
chain prerouting {
|
|
type nat hook prerouting priority dstnat; policy accept;
|
|
iifname $WAN tcp dport { 80, 443 } dnat to $PROXY
|
|
}
|
|
chain postrouting {
|
|
type nat hook postrouting priority srcnat; policy accept;
|
|
ip saddr 192.168.150.0/24 oifname $WAN masquerade
|
|
}
|
|
chain forward {
|
|
type filter hook forward priority filter; policy accept;
|
|
oifname "virbr-taller" ct state established,related accept
|
|
oifname "virbr-taller" ct status dnat accept
|
|
oifname "virbr-taller" drop
|
|
}
|
|
}
|
|
```
|
|
|
|
```sh
|
|
sudo sysctl -w net.ipv4.ip_forward=1
|
|
sudo nft -f taller.nft # recargable; no persiste tras reiniciar
|
|
sudo nft delete table ip taller # al acabar
|
|
```
|
|
|
|
El masquerade da salida a internet a la VM, porque el modo `open` no hace NAT. Si el sobremesa ya tiene un ruleset con `policy drop` en `input` o `forward`, ahí también hay que dejar pasar el DHCP y el DNS de `virbr-taller` (UDP 67 y 53) y el tráfico reenviado hacia y desde `virbr-taller`: un `drop` en cualquier tabla es definitivo.
|
|
|
|
### 5. CT proxy
|
|
|
|
```sh
|
|
pveam update
|
|
img=$(pveam available --section system | awk '/debian-13-standard_.*_amd64\./ { print $2 }' | sort -V | tail -n 1)
|
|
pveam download local "$img"
|
|
pct create 199 "local:vztmpl/$img" \
|
|
--hostname proxy --unprivileged 1 --features nesting=1 --onboot 1 \
|
|
--rootfs tank:4 \
|
|
--net0 name=eth0,bridge=vmbr0,ip=192.168.150.10/24,gw=192.168.150.1 \
|
|
--net1 name=eth1,bridge=vmbr1,ip=10.10.10.1/24
|
|
pct start 199
|
|
pct enter 199
|
|
```
|
|
|
|
Dentro del proxy:
|
|
|
|
```sh
|
|
apt update && apt install -y nginx certbot
|
|
# El sitio por defecto ya sirve /var/www/html en el 80
|
|
certbot certonly --webroot -w /var/www/html -d taller.example.org \
|
|
-m organizacion@example.org --agree-tos --deploy-hook 'systemctl reload nginx'
|
|
|
|
touch /etc/nginx/taller-routes.conf # lo rellena teams.sh; nginx -t falla si no existe
|
|
cat > /etc/nginx/sites-available/taller <<'EOF'
|
|
server {
|
|
listen 80;
|
|
server_name taller.example.org;
|
|
location /.well-known/acme-challenge/ { root /var/www/html; }
|
|
location / { return 301 https://$host$request_uri; }
|
|
}
|
|
|
|
server {
|
|
listen 443 ssl;
|
|
server_name taller.example.org;
|
|
ssl_certificate /etc/letsencrypt/live/taller.example.org/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/taller.example.org/privkey.pem;
|
|
|
|
include /etc/nginx/taller-routes.conf;
|
|
location / { return 404; }
|
|
}
|
|
EOF
|
|
ln -s ../sites-available/taller /etc/nginx/sites-enabled/taller
|
|
rm /etc/nginx/sites-enabled/default
|
|
nginx -t && systemctl reload nginx
|
|
```
|
|
|
|
### 6. Plantilla, equipos y evento
|
|
|
|
En el host Proxmox, con el repositorio copiado (`rsync -a --exclude .git ./ root@192.168.150.2:taller/`) y `DOMAIN` ajustado en `common.sh`:
|
|
|
|
```sh
|
|
./template.sh # CT 900, plantilla; descarga ttyd y la imagen de Debian 13
|
|
./teams.sh # CT 101-115, rutas de nginx y state/papelitos.html para imprimir
|
|
```
|
|
|
|
`./teams.sh 4` levanta solo los equipos 1 a 4 (para pruebas). `teams.sh` se puede repetir: salta los CT que ya existen y cada equipo conserva su token (y su papelito) aunque se recree su CT. Comprobación rápida:
|
|
|
|
```sh
|
|
# Abre en el navegador la URL de un papelito. Los CT no se ven entre sí:
|
|
pct exec 101 -- timeout 3 bash -c 'echo > /dev/tcp/10.10.10.102/7681' && echo MAL || echo bien
|
|
```
|
|
|
|
El día del evento:
|
|
|
|
```sh
|
|
./event.sh board
|
|
```
|