Adding ufw rules during a preseeded installation

Solution 1:

I have been trying to get this working for a while, too. Nothing seems to work. Until I finally gave up and I fixed it with a simple rc.local workaround:

d-i preseed/late_command string \
    mv /target/etc/rc.local /target/etc/rc.local.orig; \
    echo '#!/bin/sh -e' > /target/etc/rc.local; \
    echo '/usr/sbin/ufw allow ssh' >> /target/etc/rc.local; \
    echo 'mv -f /etc/rc.local.orig /etc/rc.local' >> /target/etc/rc.local; \
    echo 'test -x /etc/rc.local && /etc/rc.local' >> /target/etc/rc.local; \
    echo 'exit 0' >> /target/etc/rc.local; \
    chmod +x /target/etc/rc.local

The workaround will add a custom rc.local script using preseed/late_command which will:

  1. Make a backup of the original rc.local as rc.local.orig (using mv)

  2. Then new rc.local is created -- which is a script that will be executed on the first boot up of the system

  3. The new script will:

    1. Enable SSH access using ufw
    2. Restore the original rc.local (by moving rc.local.orig to rc.local, deleting itself)
    3. Test if the original rc.local is executable and run it
    4. Exit successfully