A Quick Guide to Printing on FreeBSD

This article was contributed by the FreeBSD Foundation, a US-based, non-profit organization dedicated to supporting and promoting the FreeBSD project and community worldwide. The article originally appeared on the FreeBSD Foundation website and is reposted here with permission.

Identify Connection

Printers capable of printing plain ASCII text can be quickly set up on FreeBSD.

Before starting, identify the type of connection your printer is using:

USB

When FreeBSD detects a USB printer, two device entries are created: /dev/ulpt0 and /dev/unlpt0. Data sent to either device will be relayed to the printer. unlpt0 is most commonly used as it does not reset the USB port at all, which can cause problems with some printers.

Parallel

The parallel port device is /dev/lpt0. This device appears whether a printer is attached or not, it is not autodetected.

Vendors have largely moved away from these “legacy” ports, and many computers no longer have them. Adapters can be used to connect a parallel printer to a USB port. With such an adapter, the printer can be treated as if it were actually a USB printer.

Network

Network printers are connected directly to the local computer network.

The DNS hostname of the printer must be known. If the printer is assigned a dynamic address by DHCP, DNS should be dynamically updated so that the host name always has the correct IP address. Network printers are often given static IP addresses to avoid this problem.

Most network printers understand print jobs sent with the LPD protocol. A print queue name can also be specified. Some printers process data differently depending on which queue is used. For example, a raw queue prints the data unchanged, while the text queue adds carriage returns to plain text.

Many network printers can also print data sent directly to port 9100.

Basic Setup

  • FreeBSD uses lpd(8) (Line Printer Daemon) to print in the background so the user can continue to use other programs. Start by creating a directory to store files while they are being printed:
# mkdir -p /var/spool/lpd/lp 
# chown daemon:daemon /var/spool/lpd/lp 
# chmod 770 /var/spool/lpd/lp

As root, create /etc/printcap with these contents:

lp:\
      :lp=/dev/unlpt0:\
      :sh:\
      :mx#0:\
      :sd=/var/spool/lpd/lp:\
      :lf=/var/log/lpd-errs:

**This line is for a printer connected to a USB port.

For a printer connected to a parallel or “printer” port, use:

:lp=/dev/lpt0:\

For a printer connected directly to a network, use:

:lp=:rm=network-printer-name:rp=raw:\

Replace network-printer-name with the DNS host name of the network printer.

To check for errors, run:

# chkprintcap

Enable LPD by editing /etc/rc.conf, adding this line:

lpd_enable="YES"

Start the service:

# service lpd start Starting lpd.

Print a test:

# printf "1. This printer can print.\n2. This is the second line.\n" | lpr

Direct Printing

For occasional printing, files can be sent directly to a printer device without any setup. For example, a file called sample.txt can be sent to a USB printer:

# cp sample.txt /dev/unlpt0

Direct printing to network printers depends on the abilities of the printer, but most accept print jobs on port 9100, and nc(1) can be used with them. To print the same file to a printer with the DNS hostname of netlaser:

# nc netlaser 9100 < sample.txt

Filters

The examples shown so far have sent the contents of a text file directly to the printer. As long as the printer understands the content of those files, output will be printed correctly.

Some printers are not capable of printing plain text, and the input file might not even be plain text.

Filters are specified in /etc/printcap with the if= identifier. To use /usr/local/libexec/lf2crlf as a filter, modify /etc/printcap like this:

lp:\
    :lp=/dev/unlpt0:\
    :sh:\
    :mx#0:\
    :sd=/var/spool/lpd/lp:\
    :if=/usr/local/libexec/lf2crlf:\   
    :lf=/var/log/lpd-errs:

if= identifies the input filter that will be used on incoming text.

An Example Filter

Typical FreeBSD text files contain only a single line feed character at the end of each line. These lines will “stairstep” on a standard printer:

A printed file looks
                    like the steps of a staircase
                                                 scattered by the wind

A filter can convert the newline characters into carriage returns and newlines. The carriage returns make the printer return to the left after each line. Create /usr/local/libexec/lf2crlf with these contents:

#!/bin/sh
CR=$'\r'
/usr/bin/sed -e "s/$/${CR}/g"

Set the permissions and make it executable:

# chmod 555 /usr/local/libexec/lf2crlf

Modify /etc/printcap to use the new filter:

:if=/usr/local/libexec/lf2crlf:\

Test the filter by printing the same plain text file. The carriage returns will cause each line to start at the left side of the page.

Other Printing Systems

Several other printing systems are available in addition to the built-in lpd(8). These systems offer support for other protocols or additional features.

CUPS

CUPS is a popular printing system available on many operating systems. Using CUPS on FreeBSD is documented in a separate article: CUPS

HPLIP

Hewlett Packard provides a printing system that supports many of their inkjet and laser printers. The port is print/hplip. The main web page is at https://developers.hp.com/hp-linux-imaging-and-printing. The port handles all the installation details on FreeBSD. Configuration information is shown at https://developers.hp.com/hp-linux-imaging-and-printing/install.

LPRng

LPRng was developed as an enhanced alternative to lpd(8). The port is sysutils/LPRng. For details and documentation, see http://www.lprng.com/.

FOSSlife Newsetter

Comments