前言

因为要在linux 用命令添加USB打印机,所以就有了这个博客。


文章目录

  • 前言
  • Step 1 安装 Cups
  • Step2 配置Cups 允许添加设备节点作为打印机
  • Step 3 查看USB打印机节点
  • Step 4 lpadmin 添加打印机
  • Step 5 测试打印


Step 1 安装 Cups

一般使用Ubuntu 系统的话,默认系统会安装cups 组件用来控制打印机。
如果使用lpadmin 命令提示无法找到命令,执行一下apt安装一下 cups

sudo apt-get install cups

Step2 配置Cups 允许添加设备节点作为打印机

sudo vim /etc/cups/cups-files.conf

然后在 cups-files.conf 文件中 找到 FileDevice 属性 然后 将值设置为 Yes
设置完成后的文件

#
# File/directory/user/group configuration file for the CUPS scheduler.
# See "man cups-files.conf" for a complete description of this file.
#

# List of events that are considered fatal errors for the scheduler...
#FatalErrors config

# Do we call fsync() after writing configuration or status files?
#SyncOnClose Yes

# Default user and group for filters/backends/helper programs; this cannot be
# any user or group that resolves to ID 0 for security reasons...
#User lp
#Group lp

# Administrator user group, used to match @SYSTEM in cupsd.conf policy rules...
# This cannot contain the Group value for security reasons...
SystemGroup lpadmin


# User that is substituted for unauthenticated (remote) root accesses...
#RemoteRoot remroot

# Do we allow file: device URIs other than to /dev/null?
# 设置在此处!!!!!!!!!!
FileDevice Yes

# Permissions for configuration and log files...
#ConfigFilePerm 0640
#LogFilePerm 00640

# Location of the file logging all access to the scheduler; may be the name
# "syslog". If not an absolute path, the value of ServerRoot is used as the
# root directory.  Also see the "AccessLogLevel" directive in cupsd.conf.
AccessLog /var/log/cups/access_log

# Location of cache files used by the scheduler...
#CacheDir /var/cache/cups

# Location of data files used by the scheduler...
#DataDir /usr/share/cups

# Location of the static web content served by the scheduler...
#DocumentRoot /usr/share/cups/doc-root

# Location of the file logging all messages produced by the scheduler and any
# helper programs; may be the name "syslog". If not an absolute path, the value
# of ServerRoot is used as the root directory.  Also see the "LogLevel"
# directive in cupsd.conf.
ErrorLog /var/log/cups/error_log

# Location of fonts used by older print filters...
#FontPath /usr/share/cups/fonts

# Location of LPD configuration
#LPDConfigFile 

# Location of the file logging all pages printed by the scheduler and any
# helper programs; may be the name "syslog". If not an absolute path, the value
# of ServerRoot is used as the root directory.  Also see the "PageLogFormat"
# directive in cupsd.conf.
PageLog /var/log/cups/page_log

# Location of the file listing all of the local printers...
#Printcap /run/cups/printcap

# Format of the Printcap file...
#PrintcapFormat bsd
#PrintcapFormat plist
#PrintcapFormat solaris

# Location of all spool files...
#RequestRoot /var/spool/cups

# Location of helper programs...
#ServerBin /usr/lib/cups

# SSL/TLS keychain for the scheduler...
#ServerKeychain ssl

# Location of other configuration files...
#ServerRoot /etc/cups

# Location of Samba configuration file...
#SMBConfigFile 

# Location of scheduler state files...
#StateDir /run/cups

# Location of scheduler/helper temporary files. This directory is emptied on
# scheduler startup and cannot be one of the standard (public) temporary
# directory locations for security reasons...
#TempDir /var/spool/cups/tmp

Step 3 查看USB打印机节点

使用 dmesg 命令查看一下 usb 设备插入后的设备是否能够识别成为打印机

# dmesg
[87203.338517] usb 1-1.2: Product: HP LaserJet 1020
[87203.338529] usb 1-1.2: Manufacturer: Hewlett-Packard
[87203.338543] usb 1-1.2: SerialNumber: S418K7W
[87203.340431] usblp 1-1.2:1.0: usblp0: USB Bidirectional printer dev 95 if 0 alt 0 proto 2 vid 0x03F0 pid 0x2B17

看到这个 usblp0: USB Bidirectional printer 就说明 打印机识别了,没有识别就是一个普通的usb设备,此时需要去找这个设备的驱动才可以。

在使用lsusb 查看打印机的Device id 以及绑定的usb总线

# lsusb
Bus 001 Device 095: ID 03f0:2b17 Hewlett-Packard LaserJet 1020

然后可以看到 在bus 001 上 生成的095 的设备
此时可以查看一下

# ll /dev/bus/usb/001
crw-rw-r--+ 1 root lp   189, 94 4月   4 10:57 095

看到有lp 标志 说明 这个设备是个打印机,并且device id 是 095

Step 4 lpadmin 添加打印机

先得下载驱动
https://github.com/koenkooi/foo2zjs 这个地方找到你打印机的PPD 文件。
下载好放到运行目录。

找到节点就很简单了,用lpadmin 添加打印机就好了

# lpadmin -p printer -E -v /dev/bus/usb/001/095 -P ./HP-LaserJet_1020.ppd

这样就添加了一个名称为 printer 的打印机

Step 5 测试打印

file1.pdf 必须存在

# lpr -P printer file1.pdf

此时就可以打印出内容了