Adding a Printer from the Command Line
The CUPS web tool does a good job of managing printers, even for someone who dislikes GUIs. But then there's all these cases when some automation is in order.
1 Finding Models and Printers
The lpinfo
command helps your find both models and printers. While the -m
option lists drivers for known models, the -v
option lists available devices, e.g. either directly connected to your machine or visible somewhere on the network. You need to find out about the printer and model as these two bits of information are required by lpadmin
to effectively add the printer.
The -m
option can work jointly with the --make-and-model
option to take an argument and match against model names but I usually like to leave it out and display all possible models known to the current system so I can pipe them all to other commands helping me see the forest of for the trees. You heard me come – I typically leave it to Vi to interactively perform some fuzzy searches:
lpinfo -m | vim -
You'll find that for a given model, several drivers may be available, e.g. PostScript or Gutenprint. I can't really pretend to be able to advise for one or the other. They're probably all worth a shot, taking into account available printer options that will matter to you.
2 Adding the Printer
The lpadmin
command can be tricky to operate. In particular, the meaning of options can radically change depending on the order they're supplied on the command line. That's true of -E
. For instance:
lpadmin \
-E \
-p p3005 \
-v lpd://p3005.example.com/hp-p3005 \
-m postscript-hp:0/ppd/hplip/HP/hp-laserjet_p3005-ps.ppd \
-E
- The first
-E
option forces to use encryption when connecting to the server. Note the last-E
option which means something different entirely. We'll come back to it. - The
-p
option expects the so-called destination, in other words the name of the printer to create. - Relating to
lpinfo
, thelpadmin -v
option is used for specifying the device URI, including the protocol –lpd
, here. Note that it's not the entire line aslpinfo
will normally print it out and you need to leave out the connection type, e.g.file
,direct
,file
. - Also relating to
lpinfo
, thelpadmin -m
option lets you indicate the model. It can be referred to either with whatlpinfo -m
offers to you – and there's nothing to leave out there, this time. Or it can be referred to directly with a PPD file. - The last
-E
option, this time supplied after-p
, means enable.
3 Removing the Printer
While we're on the subject of adding printers, removing them is hardly more complicated if you know where you're going. The lpadmin
man page does suggest at some point a -r
option to remove a printer from a class, but it's really the -x
option you want, which deletes the printer:
lpadmin -E -x p3005
As you can see, it's easy to get your inspiration from the command line adding a printer. You can keep -E
to use encryption. The -v
, -m
and -E
options become irrelevant. The -p
option is replaced with the -x
option.