内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档详细介绍lpadmin和lpstat命令的使用方法,
风哥提示:
包括打印机添加、配置、删除、状态查看等操作。
Part01-lpadmin命令基础
1.1 lpadmin命令语法
$ lpadmin –help
Usage: lpadmin [options] -p destination
lpadmin [options] -x destination
Options:
-d destination Set the default printer
-E Enable the destination and accept jobs
-m model Set the PPD model file/driver
-o name=value Set printer option
-p destination Specify the printer name
-R name-default Remove option from printer
-u allow:users Allow users to print
-u deny:users Deny users to print
-v device Set the device URI
-x destination Remove the printer
# 查看lpadmin版本
$ lpadmin –version
lpadmin (CUPS) 2.4.7
Part02-添加打印机
2.1 添加本地打印机
$ lpinfo -v
direct usb:/学习交流加群风哥QQ113257174/HP/LaserJet%20P1005?serial=000000000QCN7C4KQ1a
direct hp:/usb/HP_LaserJet_P1005?serial=000000000QCN7C4KQ1a
# 查看可用驱动
$ lpinfo -m | grep -i “HP LaserJet P1005”
foomatic:foo2zjs-p1005:HP LaserJet P1005 Foomatic/foo2zjs (recommended)
# 添加USB打印机
$ sudo lpadmin -p HP_LaserJet_P1005 -v “usb://HP/LaserJet%20P1005?serial=000000000QCN7C4KQ1a” -m “foomatic:foo2zjs-p1005:HP LaserJet P1005 Foomatic/foo2zjs” -E
# 验证打印机已添加
$ lpstat -p
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:45:00 CST
# 添加网络打印机(示例:IP 192.168.1.100)
$ sudo lpadmin -p Network_Printer -v “socket://192.168.1.100:9100” -m “drv:///sample.drv/generic.ppd” -E
# 验证网络打印机已添加
$ lpstat -p
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:45:00 CST
printer Network_Printer is idle. enabled since Thu 03 Apr 2026 12:46:00 CST
Part03-打印机配置
3.1 设置打印机选项
$ lpoptions -p HP_LaserJet_P1005 -l
PageSize=Letter
Resolution=600dpi
# 设置纸张大小为A4
$ sudo lpadmin -p HP_LaserJet_P1005 -o PageSize=A4
# 设置分辨率为1200dpi
$ sudo lpadmin -p HP_LaserJet_P1005 -o Resolution=1200dpi
# 设置双面打印
$ sudo lpadmin -p HP_LaserJet_P1005 -o Duplex=DuplexNoTumble
# 验证配置
$ lpoptions -p HP_LaserJet_P1005 -l
PageSize=A4
Resolution=1200dpi
Duplex=DuplexNoTumble
# 设置打印份数
$ sudo lpadmin -p HP_LaserJet_P1005 -o copies=2
# 设置打印质量
$ sudo lpadmin -p HP_LaserJet_P1005 -o print-quality=4
3.2 设置默认打印机
$ lpstat -d
system default destination: HP_LaserJet_P1005
# 设置默认打印机
$ sudo lpadmin -d Network_Printer
# 验证默认打印机已更改
$ lpstat -d
system default destination: Network_Printer
# 恢复默认打印机
$ sudo lpadmin -d HP_LaserJet_P1005
Part04-用户访问控制
4.1 允许/拒绝用户访问
$ sudo lpadmin -p HP_LaserJet_P1005 -u allow:user1,user2
# 允许特定组访问打印机
$ sudo lpadmin -p HP_LaserJet_P1005 -u allow:@printers
# 拒绝特定用户访问打印机
$ sudo lpadmin -p HP_LaserJet_P1005 -u deny:user3,user4
# 查看访问控制列表
$ lpstat -p HP_LaserJet_P1005
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:50:00 CST
Allow user1,user2,@printers
Deny user3,user4
# 移除访问控制
$ sudo lpadmin -p HP_LaserJet_P1005 -u allow:all
Part05-打印机启用/禁用
5.1 启用和禁用打印机
$ sudo cupsdisable HP_LaserJet_P1005
# 验证打印机已禁用
$ lpstat -p HP_LaserJet_P1005
printer HP_LaserJet_P1005 disabled since Thu 03 Apr 2026 12:55:00 CST –
Paused for unknown reason
# 启用打印机
$ sudo cupsenable HP_LaserJet_P1005
# 验证打印机已启用
$ lpstat -p HP_LaserJet_P1005
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:56:00 CST
# 拒绝新任务
$ sudo cupsreject HP_LaserJet_P1005
# 验证打印机已拒绝新任务
$ lpstat -a
HP_LaserJet_P1005 not accepting requests since Thu 03 Apr 2026 12:57:00 CST –
# 接受新任务
$ sudo cupsaccept HP_LaserJet_P1005
# 验证打印机已接受新任务
$ lpstat -a
HP_LaserJet_P1005 accepting requests since Thu 03 Apr 2026 12:58:00 CST
Part06-删除打印机
6.1 删除打印机配置
$ lpstat -p
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:56:00 CST
printer Network_Printer is idle. enabled since Thu 03 Apr 2026 12:46:00 CST
# 删除网络打印机
$ sudo lpadmin -x Network_Printer
# 验证打印机已删除
$ lpstat -p
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 12:56:00 CST
# 删除所有打印机
$ sudo lpadmin -x HP_LaserJet_P1005
# 验证所有打印机已删除
$ lpstat -p
no destinations
Part07-lpstat命令使用
7.1 查看打印机状态
$ lpstat -p
printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 13:00:00 CST
# 查看默认打印机
$ lpstat -d
system default destination: HP_LaserJet_P1005
# 查看打印机接受状态
$ lpstat -a
HP_LaserJet_P1005 accepting requests since Thu 03 Apr 2026 13:00:00 CST
# 查看打印任务
$ lpstat -o
HP_LaserJet_P1005-1 root 1024 Thu 03 Apr 2026 13:05:00 CST
# 查看打印机类
$ lpstat -c
no printer classes
# 查看调度器状态
$ lpstat -r
scheduler is running
# 查看所有信息
$ lpstat -t
Scheduler is running
System default destination: HP_LaserJet_P1005
Device for HP_LaserJet_P1005: usb://HP/LaserJet%20P1005?serial=000000000QCN7C4KQ1a
HP_LaserJet_P1005 accepting requests since Thu 03 Apr 2026 13:00:00 CST
Printer HP_LaserJet_P1005 is idle. enabled since Thu 03 Apr 2026 13:00:00 CST
HP_LaserJet_P1005-1 root 1024 Thu 03 Apr 2026 13:05:00 CST
Part08-实战案例
8.1 批量配置打印机
# 创建脚本批量添加打印机
$ cat > add_printers.sh << 'EOF' #!/bin/bash # 打印机列表 printers=( "Printer1:socket://192.168.1.101:9100" "Printer2:socket://192.168.1.102:9100" "Printer3:socket://192.168.1.103:9100" ) # 批量添加打印机 for printer in "${printers[@]}"; do name=$(echo $printer | cut -d: -f1) uri=$(echo $printer | cut -d: -f2) echo "Adding printer: $name" sudo lpadmin -p $name -v "$uri" -m "drv:///sample.drv/generic.更多视频教程www.fgedu.net.cnppd" -E if [ $? -eq 0 ]; then echo "Printer $name added successfully" else echo "Failed to add printer $name" fi done echo "All printers added" EOF # 赋予执行权限 $ chmod +x add_printers.sh # 执行脚本 $ ./add_printers.sh Adding printer: Printer1 Printer1 added successfully Adding printer: Printer2 Printer2 added successfully Adding printer: Printer3 Printer3 added successfully All printers added # 验证打印机已添加 $ lpstat -p printer Printer1 is idle. enabled since Thu 03 Apr 2026 13:10:00 CST printer Printer2 is idle. enabled since Thu 03 Apr 2026 13:10:00 CST printer Printer3 is idle. enabled since Thu 03 Apr 2026 13:10:00 CST
1. 使用lpadmin -E参数启用打印机
2. 配置打印机选项前先查看可用选项
3. 设置访问控制保护打印机安全
4. 定期检查打印机状态
5. 使用脚本批量管理打印机
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
