Monday, July 29, 2013

Adding a network printer with lpadmin in Linux


Configuring CUPS printers can be a bit of a pain, due to differences in URIs and drivers depending on the printers make and model. Many distributions have administrative GUIs that make printer management easier, and you can usually interact with CUPS through your browser at http://localhost:631/admin, but I prefer using the command line to get a better feel for the underlying system.

CUPS has excellent documentation. I found the sections on command line and printing options and configuring network printers particularly informative. Combined with some additional browsing and trial and error, here is the procedure to add a new default network printer, in this case an HP LaserJet 4240 at 123.45.67.89 with lpadmin.

The basic template for adding and enabling a new printer is
lpadmin -p printer-name -v device-uri -m model -L location -E

For printer-name and location, just pick something that makes sense to you. device-uri will include some extra fluff around your printer's IP address. Consult the table in the CUPS docs for ideas or search the net for ideas. For my LaserJet, the URI is socket://123.45.67.89. You can list all device types that CUPS knows about with lpinfo:


$ lpinfo -v
…
network socket
…
Finally, you'll need to figure out which model (driver) to use. lpinfo leys you search through available drivers by make and model:
$ lpinfo --make-and-model 'LaserJet 4240' -m
gutenprint.5.2://hp-lj_4240/expert HP LaserJet 4240 - CUPS+Gutenprint v5.2.5
gutenprint.5.2://hp-lj_4240/simple HP LaserJet 4240 - CUPS+Gutenprint v5.2.5 Simplified
foomatic:HP-LaserJet_4240-Postscript.ppd HP LaserJet 4240 Foomatic/Postscript
drv:///hpijs.drv/hp-laserjet_4240-hpijs-pcl3.ppd HP LaserJet 4240 hpijs pcl3, 3.10.2
lsb/usr/hplip/HP/hp-laserjet_4240-ps.ppd HP LaserJet 4240 Postscript (recommended)
Choices, choices… I've heard good things about Gutenprint, so we'll use that. Not that I ask a lot of a print driver, so perhaps it would be better to use the recommended ppd file. Just pick something. If it doesn't work, you can reconfigure with a better driver later.
On my Gentoo system, the lpinfo call failed with
lpinfo: client-error-not-found
To fix this, I had to emerge net-print/gutenprint.
Putting it all together, add and enable the new printer:
# lpadmin -E -p afmlab -v socket://123.45.67.89 -m gutenprint.5.2://hp-lj_4240/expert -L "LaserJet 4240, Disque 927" -E
Note the two -E options. The first one (before -p) forces encryption when connecting to the server. The last one enables the destination and starts accepting jobs.
You'll probably also want to make the new printer the default:
# lpadmin -d afmlab
The CUPS daemon will eventually (i.e. after a few seconds) flush these configuration changes into /etc/cups/printers.conf if you prefer editing text files to the command line or GUI tools ;).
If you want to add a printer to your local CUPS server that is already configured from another CUPS server (e.g. printing from your laptop (local CUPS) to a USB printer plugged into your desktop (another CUPS)), you can just drop the model info:
# lpadmin -E -p afmlab -v http://my-desktop:631/printers/afmlab -L "LaserJet 4240, Disque 927" -E
where my-desktop should be the domain name or IP address of the remote CUPS server.

No comments: