Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Print From Linux Using Only the Command Line

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can print from Linux entirely via the command line. You use the lp or lpr command to send files to a printer, and lpstat to check printer status. This guide walks you through finding your printer name, sending a file, and managing print jobs without ever opening a graphical interface.

What You'll Need

  • A Linux system with a terminal (any distribution works).
  • A printer that is already installed and configured on your system (either via USB, network, or as a system default). If you haven't set it up, you can use the lpadmin command, but this guide assumes the printer is already recognized.
  • Basic familiarity with the terminal (typing commands and reading output).

Step-by-Step

  1. Open a terminal. On most Linux systems, press Ctrl+Alt+T or search for "Terminal" in your applications menu.
  2. Check that your printer is detected. Run the command:
    lpstat -p -d
    This lists all printers and shows the default printer (marked with system default destination). If you see nothing, your printer isn't installed yet; you'll need to set it up first.
  3. Note your printer's name. The name is usually something like HP_LaserJet or EPSON_XP-200. If you have only one printer, you can skip specifying a name and use the default.
  4. Print a file. Use the lp command with the file path. For example:
    lp /home/user/documents/report.pdf
    This sends the file to the default printer. To specify a particular printer, add -d followed by the printer name:
    lp -d HP_LaserJet /home/user/documents/report.pdf
  5. Alternatively, use lpr. The lpr command is older but works similarly:
    lpr -P HP_LaserJet /home/user/documents/report.pdf
    Note the -P flag (capital P) for specifying the printer.
  6. Check the print job status. Run lpstat -o to see queued jobs. You'll see a job ID (like HP_LaserJet-123) and the file name.
  7. Cancel a job if needed. Use cancel with the job ID:
    cancel HP_LaserJet-123
    Or cancel all jobs with cancel -a.
  8. Print multiple files at once. Just list them all:
    lp file1.txt file2.pdf file3.odt
    They will be printed as separate jobs.
  9. Print from standard input. You can pipe text directly to lp or lpr. For example:
    cat notes.txt | lp
    Or even:
    echo "Hello, world!" | lpr
  10. Set print options. Use -o to pass options like number of copies (-o copies=2), paper size (-o media=A4), or duplex (-o sides=two-sided-long-edge). For example:
    lp -o copies=2 -o media=A4 -d printer file.pdf

Common Mistakes

  • Using the wrong printer name. The name is case-sensitive and must match exactly what lpstat -p shows. If you type HP_LaserJet but it's actually hp_laserjet, it won't work.
  • Forgetting the file path. If you're in a different directory, the file won't be found. Use absolute paths (starting with /) or cd to the correct folder first.
  • Assuming lpr uses the same syntax as lp. They are similar but not identical. lpr uses -P for printer, while lp uses -d. Double-check which command you're using.
  • Not checking the default printer. If you have multiple printers and don't specify one, your job goes to the default. If that printer is off or out of paper, the job may hang.
  • Using cancel without the job ID. cancel alone does nothing; you must specify the job ID or use -a to cancel all.

Tips

  • Set a default printer once. Use lpadmin -d printer_name (requires root) to avoid typing -d every time.
  • Print a test page. Use lp /usr/share/cups/data/testprint (if that file exists) or create your own text file with echo "test" > test.txt and print it.
  • Check printer status quickly. lpstat -t gives a full status report including whether the printer is idle or printing.
  • Use lpq to view the queue. It's a quick alternative to lpstat -o and shows the queue in a user-friendly format.
  • For PDFs, ensure you have a PDF-capable print system. Most modern Linux distributions include CUPS and a PDF filter, but if you encounter issues, install cups-pdf or similar.

Troubleshooting

If nothing prints:

  1. Check that the printer is powered on and connected (USB cable or network).
  2. Run lpstat -p to see if the printer is idle or disabled. If disabled, enable it with cupsenable printer_name (as root).
  3. Check the error log: tail -f /var/log/cups/error_log (you may need root). Look for lines mentioning your printer.
  4. Try printing a plain text file instead of a PDF to rule out format issues.

If the job is stuck in queue:

  1. Use lpstat -o to see the job ID.
  2. Cancel it with cancel job_id.
  3. Then restart the CUPS service: sudo systemctl restart cups (or sudo service cups restart).

If the printer is not listed at all:

  1. Run lsusb to see if the USB printer is detected.
  2. If it's a network printer, try pinging its IP address.
  3. Reinstall the printer driver using your distribution's package manager (e.g., sudo apt install printer-driver-* on Debian/Ubuntu).

If you get a "permission denied" error:

  1. Ensure you have permission to access the printer. On some systems, you need to be in the lp group. Add yourself: sudo usermod -aG lp $USER, then log out and back in.

Frequently Asked Questions

How do I print a PDF from the command line?

Simply run lp /path/to/file.pdf. CUPS handles PDF conversion automatically if you have the necessary filters installed (which is standard on most distributions).

Can I print to a network printer?

Yes, if the printer is already configured on your system. If not, you can add it using lpadmin with the printer's URI (e.g., ipp://192.168.1.50/ipp/print), but that requires root and specific knowledge of the printer's protocol.

How do I print double-sided from the command line?

Use the -o sides=two-sided-long-edge option with lp. For example: lp -o sides=two-sided-long-edge file.pdf. Not all printers support duplex; check your printer's capabilities.

What is the difference between lp and lpr?

Both submit print jobs, but lp is part of the System V printing system and offers more options (like -o). lpr is from the Berkeley lineage and is simpler. They are often aliases for the same underlying CUPS command.

How do I see a list of available printers?

Run lpstat -p or lpstat -a. The former shows printers and their status, the latter shows accepting jobs.

Can I print a web page from the terminal?

Yes, you can download the page with curl or wget and then print the HTML file, but the formatting may be poor. Better to use a tool like wkhtmltopdf to convert it to PDF first, then print that.