lpd: Getting the Hard Copy

by Michael Hughes

In the 16th century, the printing press opened up a whole new world of communications. Print was the first mass communications medium, paving the way for books, magazines, newspapers and all the other amenities of this generation. Although we live in a world dominated by computers and electronics, the printing press still plays an important role in our everyday lives. This fact is especially apparent if you are reading the print version of this magazine.

Today, the power of the printing press is available to the individual. Computers, printers and copy machines allow virtually anyone to communicate effectively to any number of people. Better still, it's easier and cheaper than ever.

One of the most common questions asked by newcomers to the Linux world is how to get their printers working with Linux. There are, in actuality, a few different ways to accomplish this task. First, however, you must enable lp support in the kernel and recompile. This is done in most kernels already. To check if your kernel is ready, plug your printer in and watch your kernel startup messages. If you see references to lp0 or lp1, your kernel is configured for parallel printing.

Setting it Up

The most primitive way to print a text file is simply to use the cat command to send it to the printer at /dev/lp1:

cat filename.txt > /dev/lp1

This will catenate the file (in this case, filename.txt) to /dev/lp1, your printer device. Replace /dev/lp1 with the device name of your printer, if it is different.

The main problem with printing text files this way is that most people get the dreaded “staircase” effect. It makes the printed text look


This is not acceptable, so most people use lpd (line printer daemon) to print files. If you don't have lpd already installed, it is obtainable from ftp://metalab.unc.edu/pub/Linux/system/printing/ as the lpr-linux package. Once you have downloaded and installed the software, you can add the following lines to one of your startup scripts, in order to start lpd when the system boots.

if [ -f /usr/sbin/lpd ]; then
   echo -n "Starting lpd..."
You might want to replace the path to lpd with your custom path.

However, running lpd alone isn't very useful. All it actually does is facilitate the queueing of print jobs. It does no translating or converting—that's why print filters are used. As you may know, many Linux and X applications can output and print to PostScript. This includes Netscape and the GIMP. To take advantage of these powerful applications and many others, you have to install a filter for your specific printer. Several different printer filter packages are available, so almost all popular printers are supported.

To install a filter, the first thing you must do is download the one you want. Many filters are available from the Sunsite Linux archives at ftp://metalab.unc.edu/pub/Linux/system/printing/.

The most popular print filters are apsfilter and magicfilter. In addition to the major print filters, there are many small converters and translators as well as other printer filters available for download at metalab. After you have downloaded the package you want, just follow the directions provided and install the package. (Note that metalab.unc.edu used to be sunsite.unc.edu.)

Now comes the tricky part. The lpd software reads from a configuration file called the printcap, or printer capability database. The printcap is a simple text file that holds the information necessary for lpd to output to printers. It has vast capabilities and options, but we're going to edit it in only the simplest ways right now. Fire up your favorite text editor and open the file /etc/printcap. If there is text currently in the file, make sure you don't need it (it's all commented and annotated), and comment out unneeded lines by placing a # symbol in front of the first character of those lines.

At this point, you are ready to start entering printer information into the file. Make sure you have installed your print filter correctly before proceeding.

Append this one line to your /etc/printcap file:


First, replace name with the name of your printer. Second, replace lpx with the device name of your printer, which is probably lp1. Finally, replace /path_to/print_filter with the actual path to your print filter. You must remember to change the permissions of your installed print filter so that it is executable and readable. Simply type:

chmod 755 /
Also, make sure the print spool directory, /var/spool/lpd, exists. If you want to print to a remote printer on another UNIX machine, set up the printer on that machine, then append (don't start a new line)
to the previous /etc/printcap entry shown above. If you decide to do this, however, replace :lp=/dev/lpx: with a plain :lp=:. For more options to put in the printcap file, look at the man page for printcap (type man printcap at the prompt).

Now, restart lpd by issuing the following command:

killall -HUP lpd

There should be a brief pause, then you will be dropped back to a command prompt. If you've followed these directions correctly, do a test print: fire up Netscape and print a test page. Click on the File menu and select “Print” or “Print Frame”. Make sure the Print Command field is set to

lpr -
Of course, replace printername with the name you gave your printer in /etc/printcap. There is no space between the -P and printername. To print text files, you can open them up in a program that is printer-aware (such as Netscape), or you can type the following on a command line:
lpr -
Replace printername with the printer's name and replace filename with the name of the file you want to print. At this point, if all went according to plan, there should be a nicely printed piece of paper on your printer.
Sharing Your Printers

Now that you have your printer set up for yourself, you might want to consider sharing it with the rest of the network. There are a few ways of doing this. With other UNIX or Linux machines with BSD-styled print systems, just follow the directions in the previous section, and you'll be off and running. Pay attention to the specified network configuration line.

The most likely scenario, however, is one Linux machine sharing its printer or printers with one or more Windows machines. To do this, you're going to want to use Samba, the SMB implementation for UNIX, which runs quite well on Linux. After you have finished setting up the printers, install Samba on the Linux machine. Samba is available at http://www.samba.org/ and is open-source software as well.

Read through the Samba documentation to get it installed. It's a quick install, but it does require some editing of configuration files. For those of you who are impatient, here's a simple configuration file to use (locate it at /usr/local/samba/lib/smb.conf):

   remote announce =
   interfaces =
   netbios name = your_computer_name
   workgroup = your_workgroup_name
   printing = bsd
   security = share
   comment = Public Stuff
   path = /tmp
   public = yes
   writable = yes

This will export one share for use on the network, named “public”. If you have the Win95 machines set up with the same parameters, you should be able to browse the public share and look through all of its subdirectories.

Once that is done, you're ready to add the printer. The printer is added to the config file in the following manner:

   path = /
   printer name =
   writable = yes
   public = yes
   printable = yes
   print command = lpr -Pprintername %s; rm %s

Replace printer_spool_dir with the printer's spool directory (I just use /tmp, but you can use /var/spool/lpd/ if you wish) and printername with the name of your printer (I just used hp).

At this point, restart Samba:

killall -HUP nmbd; killall -HUP smbd

Make sure you can still browse files across the network. This time, you should see a printer icon with the assigned name from the Samba configuration file.

On the Win95 clients, it would be best to install a generic PostScript printer. Then all your Win95 programs will output PostScript, and the printer filter on your Linux server will be able to both spool and print your documents as if they were local documents. To add a network printer in Win95, select the “Network” option when the Add Printer script prompts you. However, this approach doesn't always work, and you might want to use a more crude way of printing by changing the print command line in the above config file snippet to the following:

print command = cat %s > /dev/lp1; rm %s

Replace /dev/lp1 with the device to which your printer is attached. After you have replaced that line, re-install the printer on your Win95 box as the actual type; i.e., if you have a LaserJet 4L, install it as a LaserJet 4L in Win95. Note that with this method, no print spooling will take place on the Linux machine.

Wrapping Up

I hope this tutorial has helped you set up printing services in Linux. If you're fortunate enough to have a network in your home or office, you should also be able to set up the printer in question for use on the network by other computers. If you're still having trouble printing, you can check out the Linux Printing-HOWTO located at metalab.unc.edu/LDP/HOWTO/Printing-HOWTO.html. Good luck!

Michael Hughes is an honors student living in Thousand Oaks, California. His hobbies include Perl programming and snowboarding, as well as administering computers running Linux. He can be reached via e-mail at mfh@psilord.com.
Load Disqus comments