CUPS CLI Printing
CUPS CLI Printing: Terminal-Based Print Management
Overview
CUPS (Common UNIX Printing System) provides powerful command-line tools for managing printers and print jobs. This guide covers everything from adding printers to printing documents directly from the terminal.
Why CLI printing?
-
Print directly from terminal workflows
-
Script automated printing tasks
-
Manage remote printers over SSH
-
No GUI required (headless servers)
Prerequisites & Installation
Arch Linux
# Install CUPS, Avahi (mDNS discovery), and NSS plugin
sudo pacman -S cups avahi nss-mdns
# Optional: GUI management
sudo pacman -S system-config-printer
# Enable and start CUPS (localhost only by default - safe)
sudo systemctl enable --now cups
# WARNING: Avahi advertises your machine to the network by default!
# See "Security: Disable Avahi Publishing" section below before enabling.
sudo systemctl enable --now avahi-daemon
# Verify services are running
systemctl status cups
systemctl status avahi-daemon
Security Note: CUPS listens on localhost:631 only (safe). Avahi
broadcasts your machine’s presence to the local network. Configure
publishing restrictions before use - see security section below.
Debian/Ubuntu
sudo apt install cups avahi-daemon avahi-utils
sudo systemctl enable --now cups
# WARNING: See security section before enabling avahi
sudo systemctl enable --now avahi-daemon
Fedora/RHEL
sudo dnf install cups avahi avahi-tools nss-mdns
sudo systemctl enable --now cups
# WARNING: See security section before enabling avahi
sudo systemctl enable --now avahi-daemon
Configure NSS for mDNS (All Distros)
NSS (Name Service Switch) controls how your system resolves hostnames.
Adding mdns_minimal allows resolving .local hostnames (like
printer.local) via mDNS/Avahi instead of just DNS.
Edit /etc/nsswitch.conf and add mdns_minimal [NOTFOUND=return]
to the hosts line:
# Before
hosts: mymachines resolve [!UNAVAIL=return] files dns
# After
hosts: mymachines resolve [!UNAVAIL=return] files mdns_minimal [NOTFOUND=return] dns
Verify Installation
# Check CUPS is running
lpstat -r
# Check Avahi is running
avahi-browse -a # Should list services on network
# Check CUPS commands available
which lp lpstat lpadmin lpinfo
Security: Disable Avahi Publishing (Recommended)
By default, Avahi advertises your machine’s services to the network. For discovery-only (outbound queries), disable publishing:
# Edit avahi config
sudo nvim /etc/avahi/avahi-daemon.conf
# Add/modify these lines under [publish] section:
[publish]
publish-hinfo=no
publish-workstation=no
publish-domain=no
# Restart avahi
sudo systemctl restart avahi-daemon
# Verify your machine isn't advertising
avahi-browse -a -t | grep $(hostname) # Should return nothing
Quick Reference
| Task | Command |
|---|---|
List printers |
|
Print file |
|
Print to specific printer |
|
Check print queue |
|
Cancel job |
|
Cancel all jobs |
|
Set default printer |
|
Adding a Network Printer
Method 1: IPP (Internet Printing Protocol) - Recommended
# Basic syntax
sudo lpadmin -p <printer-name> -E -v ipp://<ip-address>/ipp/print -m everywhere
# Example: Add office printer
sudo lpadmin -p OfficePrinter -E -v ipp://10.50.10.113/ipp/print -m everywhere
# With description and location
sudo lpadmin -p OfficePrinter -E \
-v ipp://10.50.10.113/ipp/print \
-m everywhere \
-D "Office HP LaserJet" \
-L "Building A, Room 101"
Flags explained:
-
-p: Printer name (no spaces, use underscores) -
-E: Enable the printer and accept jobs -
-v: Device URI (printer address) -
-m everywhere: Use driverless IPP Everywhere (modern printers) -
-D: Description -
-L: Location
Method 2: Raw Socket (Port 9100)
# For printers that support raw/JetDirect
sudo lpadmin -p RawPrinter -E -v socket://10.50.10.113:9100 -m raw
Discovering Printers
Find Network Printers
# Scan for IPP printers (requires avahi)
avahi-browse -rt _ipp._tcp
# List all available printer URIs
lpinfo -v
# Filter for network printers
lpinfo -v | grep -E "ipp|socket|lpd"
# Test if printer is reachable
ping -c 2 10.50.10.113
nc -zv 10.50.10.113 9100 # Raw port
nc -zv 10.50.10.113 631 # IPP port
Printing Files
Basic Printing
# Print to default printer
lp document.pdf
# Print to specific printer
lp -d OfficePrinter document.pdf
# Print multiple files
lp -d OfficePrinter file1.pdf file2.pdf file3.pdf
# Print from stdin (pipe)
echo "Hello World" | lp
cat report.txt | lp -d OfficePrinter
# Print with title (shows in queue)
lp -t "Monthly Report" report.pdf
Print Options
# Multiple copies
lp -n 5 document.pdf
# Double-sided (duplex)
lp -o sides=two-sided-long-edge document.pdf # Portrait
lp -o sides=two-sided-short-edge document.pdf # Landscape
# Page range
lp -o page-ranges=1-5 document.pdf
lp -o page-ranges=1,3,5-10 document.pdf
# Landscape orientation
lp -o landscape document.pdf
# Fit to page
lp -o fit-to-page document.pdf
# Multiple pages per sheet (n-up)
lp -o number-up=2 document.pdf # 2 pages per sheet
lp -o number-up=4 document.pdf # 4 pages per sheet
# Paper size
lp -o media=letter document.pdf
lp -o media=a4 document.pdf
lp -o media=legal document.pdf
# Combined options
lp -d OfficePrinter -n 2 -o sides=two-sided-long-edge -o media=a4 report.pdf
Printing Different File Types
# Plain text
lp textfile.txt
# PDF (native support)
lp document.pdf
# Images
lp image.jpg
lp -o fit-to-page photo.png
# PostScript
lp document.ps
# Convert and print (using other tools)
# Markdown to PDF then print
pandoc document.md -o /tmp/doc.pdf && lp /tmp/doc.pdf
# Man page to printer
man bash | lp
# Code with syntax highlighting (requires enscript)
enscript -E -p - script.py | lp
Managing Print Queue
View Queue
# Default printer queue
lpq
# Specific printer queue
lpq -P OfficePrinter
# All printers
lpq -a
# Detailed status
lpstat -o # All jobs
lpstat -u username # Jobs by user
lpstat -p # Printer status
lpstat -t # Complete status
Printer Management
List and Status
# List all printers
lpstat -p
# Show default printer
lpstat -d
# Detailed printer info
lpstat -p -l
# Check if printer is accepting jobs
lpstat -a
Set Default Printer
# Set system default (requires sudo)
sudo lpadmin -d OfficePrinter
# Set user default (no sudo)
lpoptions -d OfficePrinter
Enable/Disable Printer
# Disable printer (stop accepting jobs)
sudo cupsdisable OfficePrinter
# Enable printer
sudo cupsenable OfficePrinter
# Reject new jobs (queue stays)
sudo cupsreject OfficePrinter
# Accept new jobs
sudo cupsaccept OfficePrinter
Printer Options
Practical Examples
Print Code Files with Line Numbers
# Using enscript (install: sudo dnf install enscript)
enscript -E -C -p - script.py | lp -d OfficePrinter
# Options:
# -E Syntax highlighting
# -C Line numbers
# -p - Output to stdout (pipe to lp)
Print with Header/Footer
# Using enscript
enscript --header='$n|Page $% of $=' -p - document.txt | lp
Troubleshooting
Check CUPS Service
# Service status
systemctl status cups
# Restart CUPS
sudo systemctl restart cups
# View logs
sudo journalctl -u cups -f
tail -f /var/log/cups/error_log
Test Printer Connection
# Ping test
ping -c 3 10.50.10.113
# Port test
nc -zv 10.50.10.113 9100 # Raw
nc -zv 10.50.10.113 631 # IPP
nc -zv 10.50.10.113 515 # LPD
# Get printer capabilities (IPP)
ipptool -tv ipp://10.50.10.113/ipp/print get-printer-attributes.test
Common Issues
“Printer not found”
# Verify printer exists
lpstat -p | grep PrinterName
# Re-add printer
sudo lpadmin -x PrinterName
sudo lpadmin -p PrinterName -E -v ipp://10.50.10.113/ipp/print -m everywhere
“Filter failed”
# Install cups-filters
sudo dnf install cups-filters
# Check file type
file document.pdf
# Try raw printing
lp -o raw document.pdf
“Access denied”
# Add user to lpadmin group
sudo usermod -aG lpadmin $USER
# Log out and back in
CUPS Web Interface
For complex configuration, CUPS provides a web interface:
# Access at
http://localhost:631
# Or enable remote access
sudo cupsctl --remote-admin
Configuration Files
| File | Purpose |
|---|---|
|
CUPS daemon config |
|
Printer definitions |
|
Printer drivers |
|
User printer options |
Quick Setup Checklist
-
Test connectivity:
ping -c 2 PRINTER_IP nc -zv PRINTER_IP 631 -
Add printer:
sudo lpadmin -p MyPrinter -E -v ipp://PRINTER_IP/ipp/print -m everywhere -
Set as default:
lpoptions -d MyPrinter -
Test print:
echo "Test page from $(hostname) at $(date)" | lp -
Verify:
lpstat -p lpq
Appendix: Printer Setup & Fun Examples
My Printer Setup
# Add printer and set as default
sudo lpadmin -p OfficePrinter -E -v ipp://10.50.10.113/ipp/print -m everywhere
lpoptions -d OfficePrinter
Printer Info: - Model: Brother MFC-J5945DW - URI:
ipp://10.50.10.113/ipp/print - Ports: 631 (IPP), 9100 (Raw)
Printing Multi-line Text (Heredoc Method)
When printing ASCII art or multi-line messages, use the heredoc method to avoid shell quoting issues:
cat > /tmp/message.txt << 'EOF'
Your message content here...
Multiple lines work fine.
Special characters are preserved.
EOF
lp -d OfficePrinter /tmp/message.txt
Example: Simple Alert Message
cat > /tmp/alert.txt << 'EOF'
================================================================================
================================================================================
██████████████████████████████████████████████████████████████████████
█ █
█ ███████ ███ ███ ███████ ██████ ██████ ███████ ███ ██ █
█ ██ ████ ████ ██ ██ ██ ██ ██ ████ ██ █
█ █████ ██ ████ ██ █████ ██████ ██ ███ █████ ██ ██ ██ █
█ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █
█ ███████ ██ ██ ███████ ██ ██ ██████ ███████ ██ ████ █
█ █
██████████████████████████████████████████████████████████████████████
CRITICAL SYSTEM ALERT
================================================================================
ATTENTION: THIS IS NOT A DRILL
Your device has been SELECTED for immediate inspection.
VIOLATION DETECTED:
- Excessive gaming hours logged
- Suspicious snack consumption patterns
- Room cleanliness below acceptable threshold
- Homework completion: UNVERIFIED
IMMEDIATE ACTION REQUIRED:
1. Step away from all electronic devices
2. Report to nearest parent IMMEDIATELY
3. Prepare for room inspection
4. Await further instructions
FAILURE TO COMPLY WILL RESULT IN:
- WiFi termination
- Device confiscation
- Extended chore assignment
================================================================================
The House Monitoring System
is always watching.
================================================================================
================================================================================
EOF
lp -d OfficePrinter /tmp/alert.txt
Example: ASCII-Only Version (Better Print Compatibility)
Use basic ASCII characters for reliable printing on any printer:
cat > /tmp/scary.txt << 'EOF'
################################################################################
# #
# W W EEEEE SSS EEEEE EEEEE Y Y OOO U U #
# W W E S E E Y Y O O U U #
# W W W EEEE SSS EEEE EEEE Y O O U U #
# WW WW E S E E Y O O U U #
# W W EEEEE SSSS EEEEE EEEEE Y OOO UUU #
# #
################################################################################
================================================================================
D A R K N E T S U R V E I L L A N C E
PARENTAL MONITORING DIVISION
================================================================================
SSSS CCCC A N N A L EEEEE RRRR TTTTT
S C A A NN N A A L E R R T
SSS C AAAAA N N N AAAAA L EEEE RRRR T
S C A A N NN A A L E R R T
SSSS CCCC A A N N A A LLLLL EEEEE R R T
+-----------------------------------------------------------------+
| |
| SUBJECT IDENTIFICATION: [REDACTED] |
| THREAT LEVEL: ########## MAXIMUM ########## |
| LOCATION: TRACED |
| STATUS: UNDER SURVEILLANCE |
| |
+-----------------------------------------------------------------+
>>> BEHAVIORAL ANOMALIES DETECTED <<<
[X] Screen time exceeded by 847%
[X] Vegetable consumption: ZERO
[X] Bed not made in 14 consecutive days
[X] Suspicious browser history detected
[X] Chores ignored: 23 instances
[X] Back-talk incidents: CLASSIFIED
>>> INITIATING PROTOCOL: CONSEQUENCES <<<
PHASE 1: WiFi password changed....... COMPLETE
PHASE 2: Gaming console locked....... COMPLETE
PHASE 3: Phone privileges revoked.... PENDING
PHASE 4: Early bedtime activated..... PENDING
PHASE 5: Extra chores assigned....... PENDING
#################################################
# #
# YOU HAVE 5 MINUTES TO COMPLY. #
# #
# REPORT TO PARENTS IMMEDIATELY. #
# #
# RESISTANCE IS FUTILE. #
# #
#################################################
___________
/ \
| 0 0 |
| __ |
| ____ |
\___________/
|||
THE HOUSE IS
WATCHING.
================================================================================
This message will self-destruct. Your move.
================================================================================
EOF
lp -d OfficePrinter /tmp/scary.txt
Example: Educational Math Worksheet (3rd Grade Variables)
cat > /tmp/math_lesson.txt << 'EOF'
################################################################################
# #
# M M AAA TTTTT H H M M AAA SSSS TTTTT EEEEE RRRR #
# MM MM A A T H H MM MM A A S T E R R #
# M M M AAAAA T HHHHH M M M AAAAA SSS T EEEE RRRR #
# M M A A T H H M M A A S T E R R #
# M M A A T H H M M A A SSSS T EEEEE R R #
# #
################################################################################
******************************************************
* *
* WELCOME TO THE WORLD OF MYSTERY NUMBERS! *
* *
* (Also Known As VARIABLES) *
* *
******************************************************
================================================================================
WHAT IS A VARIABLE?
================================================================================
A VARIABLE is like a mystery box!
+-------+
| |
| ? | <-- We don't know what's inside yet!
| |
+-------+
We use letters like X or N to represent the mystery number.
YOUR JOB: Figure out what number is hiding inside!
================================================================================
HOW TO SOLVE THE MYSTERY
================================================================================
Example: X + 3 = 7
Think: "What number plus 3 equals 7?"
+-------+
| |
| X | + 3 = 7
| |
+-------+
Count up from 3 until you reach 7:
3 ... 4 ... 5 ... 6 ... 7
+1 +2 +3 +4
You added 4! So X = 4
CHECK YOUR WORK: 4 + 3 = 7 <<< YES!
================================================================================
PRACTICE PROBLEMS
================================================================================
LEVEL 1: ADDITION MYSTERIES
~~~~~~~~~~~~~~~~~~~~~~~~~~
1) X + 2 = 5 X = _____
2) N + 4 = 9 N = _____
3) A + 6 = 10 A = _____
4) Y + 3 = 11 Y = _____
LEVEL 2: SUBTRACTION MYSTERIES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5) X - 2 = 4 X = _____
(Hint: What number MINUS 2 gives you 4?)
6) N - 5 = 3 N = _____
7) B - 4 = 6 B = _____
================================================================================
WORD PROBLEM CHALLENGES!
================================================================================
PROBLEM 1: THE COOKIE JAR
~~~~~~~~~~~~~~~~~~~~~~~~~
,---. ,---. ,---.
( @ @ )( @ @ )( @ @ )
`---' `---' `---'
Mom baked some cookies. After you ate 4 cookies,
there were 8 cookies left.
How many cookies did Mom bake?
Write it as an equation: X - 4 = 8
X = _____ cookies
PROBLEM 2: THE TOY COLLECTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
___
| | [CAR] [CAR] [CAR]
| T |
| O | Your friend gave you 5 more toy cars.
| Y | Now you have 12 toy cars total.
| S |
|___| How many did you have before?
Write it as an equation: N + 5 = 12
N = _____ toy cars
PROBLEM 3: THE STICKER BOOK
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.---.
/ \ * * *
|STICK| * * *
|ERS! | * * *
\ /
`---'
You had some stickers. You gave 7 stickers to your friend.
Now you have 15 stickers left.
How many stickers did you start with?
Write it as an equation: _______________
Answer = _____ stickers
PROBLEM 4: THE BIRTHDAY PARTY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_|___|_
| |
| HAPPY |
| B-DAY|
|_______|
There were some kids at the party.
Then 6 more friends arrived.
Now there are 14 kids at the party.
How many kids were there at first?
Equation: _______________
Answer = _____ kids
================================================================================
REMEMBER THE TRICKS!
================================================================================
FOR ADDITION PROBLEMS: X + 5 = 12
~~~~~~~~~~~~~~~~~~~~~~
Think: 12 - 5 = ? So X = 7
FOR SUBTRACTION PROBLEMS: X - 3 = 9
~~~~~~~~~~~~~~~~~~~~~~~~~
Think: 9 + 3 = ? So X = 12
+------------------------+
| |
| YOU CAN DO THIS! |
| |
| Math is just a |
| puzzle to solve! |
| |
+------------------------+
================================================================================
ANSWER KEY (No Peeking!)
================================================================================
Fold this part under or check AFTER you finish!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Level 1: 1) X=3 2) N=5 3) A=4 4) Y=8
Level 2: 5) X=6 6) N=8 7) B=10
Word Problems:
1) 12 cookies (12 - 4 = 8)
2) 7 toy cars (7 + 5 = 12)
3) 22 stickers, equation: X - 7 = 15
4) 8 kids, equation: X + 6 = 14
================================================================================
* * * GREAT JOB, MATH SUPERSTAR! * * *
================================================================================
EOF
lp -d OfficePrinter /tmp/math_lesson.txt
Tips for Printing ASCII Art
-
Avoid Unicode block characters - They may not render correctly on all printers
-
Use basic ASCII -
#,=,-,+,|,/,\,*work everywhere -
Use heredoc with quotes -
<< 'EOF'prevents shell interpretation -
Write to temp file first - Avoids quoting issues in terminal
-
Check queue -
lpqto verify job was sent
Document Version: 1.0 Last Updated: 2025-11-30 Your Printer:
10.50.10.113 (Brother MFC-J5945DW)