Printing with CUPS

Although file shares are an important feature of many Samba servers, printer shares are also important. Before you can configure a Samba printer share, though, you must have a working local printer configuration. Most Linux distributions now use the Common Unix Printing System (CUPS; http://www.cups.org) as the local printing software. Therefore, before moving on to describing Samba printer share configuration, I present some basic information on CUPS configuration.

Installing CUPS

Because CUPS is a standard part of most Linux distributions, it may be installed on your system already. Use your package tools to look for a package called cups. If it’s not installed, your computer either has no printing system installed or it uses an older printing system, such as LPRng or BSD LPD.

If your system uses an older printing system, you can either uninstall it and install CUPS in its place or forgo CUPS and use the older system instead. Samba configuration is similar in either case, and if local printing is working to your satisfaction, leaving your current printing system in place is likely to be the less troublesome solution, so you may want to skip ahead to Section 4.3. If you want to switch to CUPS, you should uninstall your existing printing software, if it’s installed. In an RPM-based distribution, the -e parameter to rpm can uninstall the software: rpm -e lprng uninstalls the lprng package, if that’s what your system uses. In Debian or its derivatives, the -r parameter to dpkg should do the job, as in dpkg -r lprng.

Once your system is shed of any printing system, the best way to install CUPS is to use a package for your distribution. These packages include SysV startup scripts designed for your distribution, as well as sample configuration files and perhaps patches. As with uninstalling programs, using your package system is usually the best approach. Some tools, such as Debian’s apt-get and Gentoo’s emerge, work over the network; for instance, typing emerge cups on a Gentoo system downloads and installs CUPS. Other tools, such as rpm on an RPM-based system or Debian’s dpkg, require that you have a package file available locally. Frequently, these files are available on your installation CD-ROMs.

If you’re using an older distribution, or one of the rare modern ones that doesn’t yet provide CUPS as at least an option, you can obtain the CUPS source code from its web page. You must then compile and install the software locally. Consult the CUPS documentation if you need help doing this.

Linux printing, like the Unix printing systems before it, typically works on a PostScript model: printers are assumed to be PostScript printers, and programs that print generate PostScript files. (Some programs with simpler printing needs generate plain-text printing output, though.) Most printers sold today aren’t PostScript models, though. For this reason, CUPS, like other printing tools, relies on Ghostscript (http://www.cs.wisc.edu/~ghost/) and related tools to convert PostScript into formats suitable for whatever printer you actually use. Frequently, installing CUPS automatically installs Ghostscript and perhaps a set of drivers and descriptions that CUPS uses to help pass data through Ghostscript. Particularly if you use a lower-level package management tool, you may need to install these tools manually before you can install CUPS; the package tool will complain about an unmet dependency if you haven’t installed the appropriate prerequisites. In some cases, you may need to search for and manually install some driver files. One site that’s likely to be helpful in this respect is the Linux Printing web site, http://www.linuxprinting.org/printer_list.cgi. Enter your printer brand and model in the selection boxes, and then click the Show button to obtain a report on the printer’s compatibility with Linux. This page may include links to special drivers for some particularly new and exotic models. In most cases, downloading the PostScript Printer Description (PPD) file is worthwhile; it’s often not necessary, but when it is, you can quickly add it to your system’s printer list.

A basic CUPS installation knows how to handle generic PostScript printers and a few common non-PostScript models from a handful of manufacturers. If you begin following the options presented in the Section 4.2.2 and can’t locate your printer model, you may need to track down and install additional drivers and/or CUPS printer descriptions. These are available from several different sources:

Your distribution

Most Linux distributions ship printer definitions in a package called cups-drivers or something similar. This package may just be a renamed version of one or more other printing packages.

Foomatic

The Linux Printing site provides a series of CUPS printer definitions at http://www.linuxprinting.org/foomatic.html. The information for particular printers on the Linux Printing site is likely to point you to a Foomatic driver for the printer.

GIMP Print

The GNU Image Manipulation Program (GIMP) is a popular graphics package for Linux, and it’s spawned a series of Ghostscript drivers and CUPS printer definitions that can be used even without the GIMP. Check http://gimp-print.sourceforge.net for more information on this package.

ESP Print Pro

Easy Software Products (ESP) developed CUPS and released it as open source software. ESP makes commercial printer definitions available, though; check http://www.easysw.com/printpro/ for details.

After you install one of these packages and restart the CUPS daemon, you should be able to select your printer by make and model in the CUPS configuration tools. In a few cases, though, you’ll need to select a compatible model. For instance, many mid-range laser printers use the Printer Control Language (PCL) created by Hewlett-Packard (HP). Thus, you may be able to get a printer working by selecting an appropriate HP LaserJet model even if yours is another make. Consult your printer’s documentation to learn if it’s compatible with a more popular model.

Adding Printers to CUPS

The simplest way to add printers to CUPS is to use the CUPS web-based administration tool. This tool runs on port 631 and is accessible by entering http://localhost:631 in a web browser running on the computer you want to configure. (You may be able to use the hostname rather than localhost, or even access a CUPS server from another computer on your network, depending on the CUPS security settings.) The result is a list of CUPS server options, such as Manage Printer Classes and Manage Jobs. Click the Manage Printers item to add, delete, or modify printer definitions. If this is the first time you’ve done this in your current session, you’ll be asked for an administrative username and password. Type root and the root password. The result should resemble Figure 4-2, although this figure shows a system with several printers already configured, and yours may show none set up.

CUPS provides a web-based tool for managing printers

Figure 4-2. CUPS provides a web-based tool for managing printers

Tip

CUPS can be configured to refuse all web-based administration features. Configuring CUPS access controls is covered in the section Section 4.2.3, so check there if you can’t get into the CUPS web-based interface.

To add a local printer using the web-based interface, follow these steps from the printer administration screen shown in Figure 4-2:

  1. Click the Add Printer button at the bottom of the printer list. (This button is below the edge of the page shown in Figure 4-2.)

  2. In the page that results, enter text for the name, location, and description of the printer. You’ll use the name you enter to refer to the printer, so make it short and descriptive, such as hp4000 if your network has just one HP 4000 printer. The location and description fields provide additional information for you and your users, so you can enter anything you like for them. When you’re done, click Continue.

  3. CUPS now asks for the printer device—the hardware port to which it’s connected. Chances are you’ll select a parallel or USB port. You can also select various network printer options, should you want to configure a Linux system to use a remote printer queue. For this description, I assume you’re configuring a parallel printer; some options (particularly for network printers) deviate somewhat from this description. In particular, you must enter a network path for a network printer device, such as ldap://server/queue for an LPD printer. Click Continue when you’re done.

  4. CUPS presents you with a list of printer makes. If you’ve installed Foomatic, GIMP Print, or some other printer description package, this list will be fairly long, and chances are your make will be available. If not, either install additional packages and restart the CUPS daemon or select a compatible make, such as HP or HEWLETT-PACKARD if you’re using a PCL printer. Click Continue when you’re done.

  5. CUPS presents you with a list of printer models. Select yours. Sometimes you may need to pick between two drivers for a single model. You can pick the one that’s flagged as being recommended, configure two queues so you can try both, or consult a source such as the Linux Printing web site for advice. If you don’t see your model, either pick a compatible one or use your browser’s back button to look for an alternate spelling for your printer make. When you’ve selected a model, click Continue.

  6. CUPS should respond that the printer has been added. You can then click its name to bring up a control panel for the printer. Figure 4-2 shows these options for three printers.

  7. Click Print Test Page to test the printer’s functionality. After a brief delay, the printer should spring into action and print a test page.

Of course, the CUPS web interface, like other Linux GUI administration tools, works by modifying text files. In the case of CUPS, most configuration files reside in /etc/cups and its subdirectories. In particular, the file printers.conf defines the actual printers. If you prefer to directly modify the configuration file yourself, you can do so by editing this file. Printer definitions begin with the string Printer or DefaultPrinter (the latter is for the default printer; there should be only one of these) followed by the printer name and enclosed in angle brackets (<>). Subsequent lines set the options entered in the web-based tool; each begins with a keyword and continues with a string of some sort. A few options, such as State, are adjusted by CUPS as it operates. An example entry looks like this:

<DefaultPrinter hp4000>
Info Hewlett-Packard HP LaserJet 4000 Series
Location Parallel printer on /dev/lp1
DeviceURI parallel:/dev/lp1
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>

As a general rule, it’s easiest to create printer definitions using the CUPS web-based tool. Directly editing the printers.conf file can be tricky because you must know the syntax for all of the options, as well as option names. Directly editing the file can be a handy way to quickly make a change, though, such as changing the default printer.

Adjusting CUPS Browsing and Security

In addition to printers.conf, another CUPS configuration file is critically important: cupsd.conf . This file, which also resides in /etc/cups, controls overall server operations, including security features and browsing. In the context of CUPS, browsing refers to the automatic discovery of printers that are shared via the Internet Printing Protocol (IPP), the printing protocol used by CUPS for CUPS-to-CUPS printer sharing. One of the main advantages of CUPS over earlier Linux printing systems is that CUPS supports browsing. This fact can greatly simplify printer configuration on Linux clients: just ensure that the server and client support browsing, and the client will automatically detect remote printers and add them to its printer list. If you subsequently add a printer to a CUPS server, you won’t need to touch the clients; they’ll detect the new printers within a matter of minutes.

To enable browsing on a CUPS server, open cupsd.conf in a text editor, and look for the Browsing line, which will probably be about half-way through the file:

Browsing On

The default value for this option is On, meaning that browsing is enabled. Some distributions set this parameter to Off, though, presumably as a security precaution. If you want a server to announce its printers to other computers, be sure that Browsing is set to On.

Tip

After you make a change to the CUPS configuration file and restart the CUPS daemon, it may take several seconds for clients to see the new printers.

You may also want to adjust the BrowseAddress lines, which appear shortly after the Browsing option. This parameter specifies the network addresses to which browse lists should be broadcast. On a small LAN, a value of @LOCAL should do well. This option tells CUPS to broadcast browse lists to its local network interfaces. You can also specify network blocks using broadcast IP addresses, as in 192.168.17.255 to send broadcasts to the 192.168.17.0/24 network. In fact, you can include several BrowseAddress lines to specify multiple networks:

BrowseAddress @LOCAL
BrowseAddress 192.168.17.255

On the client side, the BrowseAllow and BrowseDeny options specify addresses from which browse packets should or should not be accepted. You can set these options as DNS hostnames, as domain names preceded by dots (as in ldap://.example.com), as IP addresses, as IP address/netmask pairs, or in various other forms. You can also use the same @LOCAL notation accepted by BrowseAddress. Typically, you’ll set a CUPS client to accept browse packets from your local network, and perhaps from some others:

BrowseAllow @LOCAL
BrowseAllow 192.168.17.0/24

The default for BrowseAllow is All, which accepts all browse packets. This configuration is appropriate for CUPS clients on small LANs, but if you’re configuring a system on a larger network, you may want to restrict the remote printers it adds by explicitly setting BrowseAllow.

Most of the options in cupsd.conf are global; they apply to the CUPS configuration as a whole. CUPS also supports more fine-grained control in sections that are delimited by Location directives. These begin with the keyword Location and a location name, surrounded by angle brackets (<>), and end with </Location>:

<Location /printers>
Order Deny,Allow
Deny From All
Allow from 127.0.0.1
Allow from 192.168.1.0/24
Allow from 172.24.21.0/24
Allow From @LOCAL
</Location>

This example uses the Allow directive to tell CUPS to grant access to the specified computers and networks. This example also controls access to the /printers location, which, as you might expect, provides the means to access printers. Other locations of interest include the root (/), which sets the default security policy; /jobs, which controls the ability to adjust job priorities, delete jobs, and so on; and /admin, which controls administrative functions such as adding new printers. This last section frequently includes AuthType and AuthClass options, which tell CUPS to require authentication; after all, you don’t want to let just anybody access the printer administration tools.

Tip

If you can’t access the CUPS administration web page, try examining the /admin location. Be sure that an Allow line grants access to the computer used to access the server. Frequently, this is set to 127.0.0.1, for localhost access only, but you can add more lines to grant access to other computers. I recommend doing so with caution, though; granting other computers access to a CUPS server is a potential security risk.

Get Linux in a Windows World now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.