Tuesday 6 April 2021

Sharing printers via AirPrint

I had been struggling to get my old Canon MP560 to work with my new Mac Mini running Bir Sur. I decided to use my Raspberry Pi 4 running Ubuntu Server 20.04 as a printer server and at the same time to see if I could finally get printing to work from my iPhone.

I discovered that it is relatively straight forward to setup printer sharing via AirPrint. I based my inital attempt on the Linux Magazine's atricle on setting up AirPrint but I have different settings for the cups configuration and didn't need to add the mime types. I also needed to adjust the firewall settings and get my printers working from Linux as I had not previously set them up for Linux, only for Windows and MacOS

CUPS installation

For the CUPS installation I used the following packages:

  • cups-bsd
  • avahi
  • printer-driver-gutenprint

I installed CUPS and the Gutenprint printer drivers (which include drivers for both my Canon MP560 and HP Laserjet 6P) using the following steps:

sudo apt-get update sudo apt-get install cups-bsd avahi-daemon printer-driver-gutenprint sudo systemctl start cups sudo systemctl enable cups

I then edited the CUPS configuration and changed only the bits shown below. I left the rest of the file alone.

# Only listen for connections from the local machine. Port 631 Listen /run/cups/cups.sock # Show shared printers on the local network. Browsing On BrowseLocalProtocols dnssd # Default authentication type, when authentication is required... DefaultAuthType Basic # Web interface setting... WebInterface Yes # Restrict access to the server... <Location /> Order allow,deny Allow @LOCAL </Location> # Restrict access to the admin pages... <Location /admin> AuthType Default Require valid-user Order allow,deny Allow @LOCAL </Location> # Restrict access to configuration files... <Location /admin/conf> AuthType Default Require user @SYSTEM Order allow,deny </Location>

I then configured the firewall to allow access to CUPS and Avahi. Note the network this setup is on is 192.168.200.0 as seen in the config below - change as required.

sudo ufw allow in from 192.168.200.0/24 to any port 631 sudo ufw allow 5353/udp

I then added any required users to the lpadmin group. eg sudo adduser XXX lpadmin before restarting CUPS (sudo systemctl restart cups) and starting and enabling avahi-daemon.

sudo systemctl start avahi-daemon sudo systemctl enable avahi-daemon sudo systemctl restart cups

I then connected to my Raspberry Pi on port 631 and used the CUPS admin interface to add my Canon MP560 and HP Laser 6P printers and checked that they worked by printing a test page for each.

AirPrint configuration

Once I had confirmed that the printers were setup and that the Raspberry Pi could successfully print from them, I used the 'airprint-generate.py' script from https://github.com/tjfontaine/airprint-generate/archive/refs/heads/master.zip to generate avahi service files. 'airprint-generate.py' has the following dependencies:

  • python3
  • python3-cups
sudo apt-get install python3 python3-cups unzip wget https://github.com/tjfontaine/airprint-generate/archive/refs/heads/master.zip unzip master.zip cd airprint-generate-master/ ./airprint-generate.py

Both of my printers were now visible to and usable from both my iPhone and Mac Mini.