How to Print From Linux Using Only the Command Line
In this article
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
lpadmincommand, but this guide assumes the printer is already recognized. - Basic familiarity with the terminal (typing commands and reading output).
Step-by-Step
- Open a terminal. On most Linux systems, press Ctrl+Alt+T or search for "Terminal" in your applications menu.
- Check that your printer is detected. Run the command:
lpstat -p -d
This lists all printers and shows the default printer (marked withsystem default destination). If you see nothing, your printer isn't installed yet; you'll need to set it up first. - Note your printer's name. The name is usually something like
HP_LaserJetorEPSON_XP-200. If you have only one printer, you can skip specifying a name and use the default. - Print a file. Use the
lpcommand 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-dfollowed by the printer name:lp -d HP_LaserJet /home/user/documents/report.pdf - Alternatively, use
lpr. Thelprcommand is older but works similarly:lpr -P HP_LaserJet /home/user/documents/report.pdf
Note the-Pflag (capital P) for specifying the printer. - Check the print job status. Run
lpstat -oto see queued jobs. You'll see a job ID (likeHP_LaserJet-123) and the file name. - Cancel a job if needed. Use
cancelwith the job ID:cancel HP_LaserJet-123
Or cancel all jobs withcancel -a. - Print multiple files at once. Just list them all:
lp file1.txt file2.pdf file3.odt
They will be printed as separate jobs. - Print from standard input. You can pipe text directly to
lporlpr. For example:cat notes.txt | lp
Or even:echo "Hello, world!" | lpr - Set print options. Use
-oto 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 -pshows. If you typeHP_LaserJetbut it's actuallyhp_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
/) orcdto the correct folder first. - Assuming
lpruses the same syntax aslp. They are similar but not identical.lpruses-Pfor printer, whilelpuses-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
cancelwithout the job ID.cancelalone does nothing; you must specify the job ID or use-ato cancel all.
Tips
- Set a default printer once. Use
lpadmin -d printer_name(requires root) to avoid typing-devery time. - Print a test page. Use
lp /usr/share/cups/data/testprint(if that file exists) or create your own text file withecho "test" > test.txtand print it. - Check printer status quickly.
lpstat -tgives a full status report including whether the printer is idle or printing. - Use
lpqto view the queue. It's a quick alternative tolpstat -oand 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-pdfor similar.
Troubleshooting
If nothing prints:
- Check that the printer is powered on and connected (USB cable or network).
- Run
lpstat -pto see if the printer isidleordisabled. If disabled, enable it withcupsenable printer_name(as root). - Check the error log:
tail -f /var/log/cups/error_log(you may need root). Look for lines mentioning your printer. - Try printing a plain text file instead of a PDF to rule out format issues.
If the job is stuck in queue:
- Use
lpstat -oto see the job ID. - Cancel it with
cancel job_id. - Then restart the CUPS service:
sudo systemctl restart cups(orsudo service cups restart).
If the printer is not listed at all:
- Run
lsusbto see if the USB printer is detected. - If it's a network printer, try pinging its IP address.
- 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:
- Ensure you have permission to access the printer. On some systems, you need to be in the
lpgroup. 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.