Linux Exercise: Manage Printers and Printing

Configure CUPS

  1. If you are not using a graphical interface at the moment, start it.
    • # startx
  2. Open a terminal window and run the tty command in here. This will print the name of the device that represents this terminal. Usually this is /dev/pts/something. Remember this name. Set the permissions of this tty to 777. Leave this window open but don't use it anymore: We're going to use it as a simulated printer.
    • # tty
    • # chmod 777 /dev/pts/something
  3. To make it possible to use a tty as a simulated prnter, open another terminal window, change the file /etc/cups/cups-files.conf. Set FileDevice to Yes. Then restart CUPS.
    • # vi /etc/cups/cups-files.conf
      ...
      FileDevice Yes
      ...
    • # systemctl restart cups
  4. Also disable SELinux. This would otherwise prevent CUPS from writing to /dev/pts/something.
    • # setenforce 0
  5. Start a browser and go to http://localhost:631.
  6. In the CUPS interface, add a printer. Use as connection "file:///dev/pts/something". Use the Generix Text Printer driver.

Printing text files

  1. Use the lpr command to print the file /etc/passwd. Does this one appear on the virtual printer terminal?
    • # lpr -P printername /etc/passwd

Managing Printer Queues

  1. Stop your printer queue.
    • # cupsdisable printername
  2. Submit another job to the printer.
    • # lpr -P printername /etc/passwd
  3. View which jobs are waiting to be printed.
    • # lpq -P printername
  4. Start the printer queue again. Do the jobs continue? Look at the printer queue again.
    • # cupsenable printername
    • # lpq -P printername

Other printer commands (optional)

If time permits, spend some time playing with lpstat, lprm, cupsaccept, cupsreject and cupsctl.

Cups-PDF printer (optional)

  1. In the cups-pdf RPM is the configuration and code for an PDF printer: Everything you print to this printer will be converted to a PDF file and stored on your Desktop (which is really the ~/Desktop directory). Install this RPM.
    On Red Hat/CentOS 7, this RPM is not available in the CentOS repository, but comes from the EPEL repository. If you have not done so before, you need to make sure EPEL is enabled as well.
    On Red Hat/CentOS 8, the Cups-PDF RPM is not available in EPEL, but you can use the RPM from EPEL for Red Hat/CentOS 7. This RPM can also be downloaded here.
    • Red Hat / CentOS 7:
      # yum -y install epel-release
      # yum install cups-pdf
    • Red Hat / CentOS 8:
      # curl -O https://www.demo.wlid.nl/sources/cups-pdf-2.6.1-7.el7.x86_64.rpm
      # yum localinstall cups-pdf-2.6.1-7.el7.x86_64.rpm
  2. Now print the file /etc/passwd to this Cups-PDF printer. Look at the Desktop of the user who started this job, or in the directory ~/Desktop. Did you see the PDF file?
    • # lpr -P Cups-PDF /etc/passwd
    • # ls -l ~/Desktop

CUPS management via SSH local tunnels (optional)

If you don't have or want a graphical environment on the system that's running your CUPS daemin, then you can also reach port 631 from a browser elsewhere. But this can normally not be done directly, as CUPS is only listening to the loopback interface. Of course you can open up CUPS to the world, but this is not very secure. An alternative solution is to use an SSH local tunnel.

A local tunnel means that your SSH client, in addition to setting up the SSH connection, also starts listening to a local port. Any traffic that is received on this local port is forwarded over the SSH connection to a remote system/port.

In Linux, an SSH session with a local tunnel is setup using the following command:

# ssh -L 1234:127.0.0.1:631 user@remotehost

You run this command on the system which is also going to run your browser: Your own workstation. The SSH opens a normal login connection like we all know and love. But in addition to this, it starts listening to the local port 1234. Any traffic arriving here is tunneled through the SSH connection to the SSH daemon. The SSH daemon in turn sets up a connection to 127.0.0.1 port 631 and forwards all traffic.

Once the tunnel is up, you can reach the CUPS daemon by starting your browser and pointing it to http://localhost:1234.

PuTTy also supports local tunnels. In order to set this up, go to the configuration of your PuTTy session: Connection; SSH; Tunnels. Choose Local Tunnel, port 1234, destination 127.0.0.1:631. Then save the configuration.

End of exercise