The lpadmin command in Linux/Unix systems is a powerful tool for managing printers, particularly in environments using the CUPS (Common Unix Printing System) printing system. It allows you to add, modify, delete, and configure printers, as well as manage printer classes, options, and defaults. Below is a detailed guide on how to manage printers using lpadmin.
Prerequisites
-
CUPS Installation: Ensure CUPS is installed and running on your system. You can check this with:
sudo systemctl status cups
If it's not installed, install it (e.g., on Debian/Ubuntu):
sudo apt install cups
-
Administrative Privileges: Most lpadmin commands require root or sudo privileges.
-
Printer Information: Have the following details ready for your printer:
- Printer name (a unique identifier for the printer).
- Printer URI (e.g.,
ipp://printer-ip/ipp, usb://HP/DeskJet, etc.).
- Printer driver or PPD (PostScript Printer Description) file, if needed.
Common lpadmin Commands
1. Adding a Printer
To add a new printer, use the -p option to specify the printer name, -v for the device URI, and -m for the driver/PPD file.
Example:
sudo lpadmin -p MyPrinter -v ipp://192.168.1.100/ipp -m everywhere -E
-p MyPrinter: Specifies the printer name.
-v ipp://192.168.1.100/ipp: Specifies the printer's URI (replace with your printer's URI).
-m everywhere: Uses the "everywhere" driver (for IPP Everywhere printers). Alternatively, specify a PPD file (e.g., -m /path/to/printer.ppd).
-E: Enables the printer and accepts jobs.
2. Listing Available Printers
To see all configured printers, use the lpstat command (not lpadmin, but complementary):
lpstat -p -d
-p: Lists all printers and their status.
-d: Shows the default printer.
3. Setting a Default Printer
To set a printer as the system default, use the -d option:
sudo lpadmin -d MyPrinter
-d MyPrinter: Sets MyPrinter as the default printer.
4. Modifying a Printer
To modify an existing printer (e.g., change its URI, driver, or options), use the -p option with the new settings.
Example (changing the URI):
sudo lpadmin -p MyPrinter -v ipp://192.168.1.101/ipp
Example (changing the driver/PPD):
sudo lpadmin -p MyPrinter -m /path/to/new-printer.ppd
5. Deleting a Printer
To remove a printer, use the -x option:
sudo lpadmin -x MyPrinter
-x MyPrinter: Deletes the printer named MyPrinter.
6. Enabling/Disabling a Printer
To enable a printer (allow it to accept jobs):
sudo lpadmin -p MyPrinter -E
To disable a printer (stop it from accepting jobs):
sudo lpadmin -p MyPrinter -r
7. Setting Printer Options
You can configure printer-specific options (e.g., paper size, duplex printing) using the -o option.
Example (setting default paper size to A4):
sudo lpadmin -p MyPrinter -o media=A4
Example (enabling duplex printing):
sudo lpadmin -p MyPrinter -o sides=two-sided-long-edge
To see available options for a printer, use the lpoptions command:
lpoptions -p MyPrinter -l
8. Creating Printer Classes
Printer classes allow you to group multiple printers so jobs can be sent to any available printer in the class. Use the -c option to add a printer to a class.
Example (creating a class and adding printers):
sudo lpadmin -p Printer1 -c OfficePrinters
sudo lpadmin -p Printer2 -c OfficePrinters
-c OfficePrinters: Adds the printer to the OfficePrinters class. If the class doesn't exist, it will be created.
To remove a printer from a class:
sudo lpadmin -p Printer1 -r OfficePrinters
9. Removing a Printer Class
To delete a printer class, use the -x option with the class name:
sudo lpadmin -x OfficePrinters
10. Managing Printer Access
You can restrict access to a printer using the -u option to allow or deny specific users.
Example (allowing specific users):
sudo lpadmin -p MyPrinter -u allow:user1,user2
Example (denying specific users):
sudo lpadmin -p MyPrinter -u deny:user3
Example (allowing all users):
sudo lpadmin -p MyPrinter -u allow:all
11. Checking Printer Status
To check the status of a printer, use lpstat:
lpstat -p MyPrinter
12. Testing a Printer
To test a printer, you can send a test page using the lp command:
lp -d MyPrinter /path/to/testfile.txt
Alternatively, use the CUPS web interface (see below) to print a test page.
Finding Printer URIs
To find the correct URI for your printer, use the lpinfo command:
lpinfo -v
This will list all available printer URIs (e.g., USB, network, IPP, etc.).
Finding PPD Files
PPD files describe the capabilities of a printer. You can find available PPDs with:
lpinfo -m
Alternatively, download the PPD file from the printer manufacturer's website or use the CUPS web interface to select a driver.
Using the CUPS Web Interface
For a more user-friendly approach, you can manage printers via the CUPS web interface:
- Open a web browser and go to
http://localhost:631.
- Navigate to the "Administration" tab.
- Use the interface to add, modify, or delete printers.
Note: You may need to authenticate with your system username and password.
Troubleshooting
- Printer Not Found: Ensure the printer is powered on, connected, and the URI is correct. Use
lpinfo -v to verify.
- Permission Denied: Run
lpadmin commands with sudo.
- Driver Issues: Ensure the correct PPD file or driver is installed. Check
/usr/share/cups/model/ for available PPDs.
- CUPS Logs: Check CUPS logs for errors:
sudo less /var/log/cups/error_log
Example Workflow
Here’s an example of setting up a network printer from scratch:
-
Find the printer URI:
lpinfo -v
Output: network ipp://192.168.1.100/ipp
-
Add the printer:
sudo lpadmin -p OfficePrinter -v ipp://192.168.1.100/ipp -m everywhere -E
-
Set it as the default printer:
sudo lpadmin -d OfficePrinter
-
Test the printer:
lp -d OfficePrinter /etc/motd
Conclusion
The lpadmin command is a versatile tool for managing printers in a CUPS environment. By combining it with other CUPS utilities like lpstat, lpinfo, and lpoptions, you can fully configure and troubleshoot your printing setup. For more complex setups or a graphical interface, consider using the CUPS web interface or a desktop printer management tool.