知识的荒漠

不积跬步无以至千里,不积小流无以成江海;千里之行,始于足下!

用户工具

站点工具


服务器:nas:cups

来源:Set Up CUPS Print Server on Ubuntu (Bonjour, IPP, Samba, AirPrint)
This tutorial will be showing you how to share a printer attached to an Ubuntu computer with Windows, macOS and iOS clients on the same network. CUPS (Common Unix Printing System) is the default printing system on Linux, FreeBSD, and macOS. Your Linux desktop environment may have a dedicated printer configuration utility, but they all use CUPS under the hood.
CUPS printer can be shared on the network using several protocols, including:

  • Bonjour+IPP:Bonjour,alsoknownasmDNS/DNS-SD(multicastDNS/DNSservicediscovery),allowsacomputertofindservicesonthelocalnetwork.IPP(InternetPrintingProtocol)isthetransportprotocol.
  • SMB:akaSamba,mainlyusedtosharefilesandprinterswithWindowsclients.
  • AirPrint:AllowsiPhone,iPad,andmacOSclientstoprintoverWi-Fi.

Each protocol has its advantages and disadvantages. First, I will show you how to install and configure CUPS. Then we will learn how to share the CUPS printer via the above 3 protocols. I recommend using all 3 methods to share your printer, so users can find an available printer on the local network with minimal effort.

Step1:在ubuntu上安装cups

Ubuntu desktop edition has CUPS pre-installed. If you use Ubuntu server edition, you need to run the following command to install CUPS from the default Ubuntu repository.

sudo apt install cups
sudo systemctl start cups
sudo systemctl enable cups
systemctl status cups

Sample output:

Next, edit the CUPS main configuration file with a command-line text editor like Nano.

sudo nano /etc/cups/cupsd.conf
#First, we need to show shared printers on the local network. Find the following line.
Browsing Off
#Change it to
Browsing On

so other computers in the same network can see printers connected to your Ubuntu computer.
By default, the CUPS web interface is only available at localhost:631. If you are running Ubuntu server edition, you may also want to make CUPS listen on all available network interface, so that you will be able to access the CUPS web interface from other computers. Find the following line.

Listen localhost:631
#Change it to
Port 631

So CUPS will listen on all network interfaces.Then find the following lines.

<Location />
  Order allow,deny
</Location>

The above configuration only allows access to the CUPS web interface from localhost. To allow access from other computers in the same network, add Allow @LOCAL to the configuration like below.

<Location />
  Order allow,deny
  Allow @LOCAL
</Location>

If you want to allow remote administration from local network, also add it for the /admin directory.

<Location /admin>
  Order allow,deny
  Allow @LOCAL
</Location>

Save and close the file. Then restart CUPS for the changes to take effect.

sudo systemctl restart cups

Note that if you have enabled the UFW firewall on Ubuntu, you need to allow clients in the same network to access port 631 on your Ubuntu box. For example, my private network is using the 192.168.0.0 ~192.168.0.255 network range, so I run the following command.

sudo ufw allow in from 192.168.0.0/24 to any port 631

Step2:在ubuntu上安装打印机驱动

You need to install driver on Ubuntu, so it can recognize and use the printer. If you have an HP printer, you can easily install the driver with the following command.

sudo apt install hplip

I also recommend installing the printer-driver-gutenprint package, which provides CUPS drivers for Canon, Epson, HP and compatible printers.

sudo apt install printer-driver-gutenprint

If you have other printers, you can find drivers on openprinting.org.
After installing the driver, you may need to re-connect the printer to the USB port of your Ubuntu computer. To test if the driver is working correctly, you can create a text file on Ubuntu:

echo "LinuxBabe is awesome!" > file.txt

Then run the following command to print this text file from the command line.

lp file.txt

This is a very rudimentary method, so don’t worry about printing quality now.

Step3:通过Bonjour/IPP协议共享CUPS打印机

Installing Avahi-daemon
CUPS can announce its presence on the network via mDNS (multicast DNS) and DNS-SD (DNS Service Discovery) protocol, which is also known as Bonjour. In order to do that, you need to install and run avahi-daemon, which is a service similiar to the Apple Bonjour service that allows computers to automatically discover shared devices and services on the local network.

sudo apt install avahi-daemon
sudo systemctl start avahi-daemon
sudo systemctl enable avahi-daemon
#Avahi-daemon listens on UDP port 5353\. Open it in the firewall.
sudo ufw allow 5353/udp

IPP Driverless Printing
Bonjour is used to advertise the printer on the local network. To make clients and the CUPS server communicate with each other, IPP (Internet Printing Protocol) is needed. The advantage of IPP is that clients can use the shared printer without installing any driver on their own devices. CUPS supports IPP out of the box, so you don’t need to do anything else to share CUPS printer via IPP.

Step4:在客户端计算机上添加打印机

macOS and Linux Clients
Because macOS and most Linux desktop distributions have CUPS installed as the default printing system, once you have enabled printer sharing via Bonjour/IPP on the Ubuntu box, macOS and Linux users in the same network can automatically use the printer. When they click the print option in applications (word processors, email readers, photo editors, and web browsers), the printer will be automatically available. They don’t have to explicitly add the printer. It’s magic.
If your Linux computer can’t find the printer, it’s possible that your system doesn’t have the ippfind command. Run the following command to install it on Debian-based Linux distribution.

sudo apt install cups-ipp-utils

On CentOS 8, run the following command.

sudo dnf install cups-ipptool

Then restart CUPS on the client computer.

sudo systemctl restart cups

Windows
Windows 10 ships with an IPP client. Type in printer in the lower-left search bar and open Printers & Scanners. Then click the Add a printer or scanner button. It will scan available printers on the local network.

As you can see, it found my HP Deskjet printer. Select the found printer and click Add device. It will be added to the printer list in a few moments.

If you are using a different version of Windows that can’t add printer this way, then you can install the Bonjour Print services. Once installed, launch the Bonjour printer wizard. It will automatically scan available printers on the local network. As you can see from the screenshot, it found my HP printer.

Click next, then you need to choose a driver for this printer. You can choose the Microsoft IPP class driver, which is installed on the system by default.

Click Next, and the printer will be added to your Windows system.
Manually Adding Printer on Linux
If for any reason you don’t see the printer, you can manually add one. To add a Bonjour-shared printer on desktop Linux, search your system settings or the application menu for the printer configuration utility. Click the Add button to add a new printer.

Then click Network Printer, and it would automatically scan available printers on the local network. As you can see, it found my HP Deskjet printer. Click the Forward button.

Then you can give the printer a name and description. I simply accept the default values. Click Apply and you are done.

Manually Adding Printer on macOS
To add a Bonjour-shared printer on macOS, go to system preferences → Printers & Scanners. Click the plus (+) button to add a printer.

It would automatically scan available printers on the local network. As you can see, it found my HP Deskjet printer.

Click the Add button and it will appear in the printer list.

Step5:通过Samba共享CUPS打印机

Samba is a free and open-source SMB/CIFS protocol implementation for Unix and Linux that allows for file and print sharing between Unix/Linux and Windows machines in a local area network. It’s mainly used to share files and printer with Windows clients.
To install Samba on Ubuntu, simply run the following command in terminal.

sudo apt install samba samba-common-bin

To check if Samba service is running, issue the following commands.

systemctl status smbd
systemctl status nmbd

To start these two services, issue the following commands:

sudo systemctl start smbd
sudo systemctl start nmbd

Then edit the main configuration file.

sudo nano /etc/samba/smb.conf

It’s recommended to enable the spoolssd service when sharing printer. This will make Samba more efficient when there’s lots of printing jobs. Simply add the following two lines in the [global] section to enable the spoolssd service.

 rpc_server:spoolss = external
 rpc_daemon:spoolssd = fork

Next, go to the end of the file and you will see the [printers] section. In Nano text editor, you can jump to the end of a file by pressing Ctrl+W, then Pressing Ctrl+V. Find the following two lines.

browseable = no
guest ok = no

Change them to

browseable = yes
guest ok = yes

Save and close the file. Then restart Samba.

sudo systemctl restart smbd nmbd

Adding a Samba-shared Printer in Windows.
Open file explorer, enter the IP address of the Ubuntu computer in the address bar like \\192.168.0.110. The printer should now be listed.

Double-click the printer to add it to your Windows system. Then click OK button to select a driver to install. After installing the driver, the printer will be added to your Windows system.

Step6:通过AirPrint与iOS客户端共享CUPS打印机

AirPrint allows iPhone, iPad, and macOS clients to print over Wi-Fi without installing driver software on the client devices. CUPS supports AirPrint, but avahi-daemon by default doesn’t announce AirPrint service on the local network. We need to create a .service file in the /etc/avahi/services/ directory for the printer with a Python script with the following command. My printer’s model is DeskJet 2130 series. Replace it with your own model name.

sudo nano /etc/avahi/services/AirPrint-DeskJet-2130-series.service

Add the following lines in the file.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">AirPrint DeskJet-2130-series @ %h</name>
  <service>
<type>_ipp._tcp</type>
<subtype>_universal._sub._ipp._tcp</subtype>
<port>631</port>
<txt-record>txtvers=1</txt-record>
<txt-record>qtotal=1</txt-record>
<txt-record>Transparent=T</txt-record>
<txt-record>URF=none</txt-record>
<txt-record>rp=printers/DeskJet-2130-series</txt-record>
<txt-record>note=HP DeskJet 2130 series</txt-record>
<txt-record>product=(GPL Ghostscript)</txt-record>
<txt-record>printer-state=3</txt-record>
<txt-record>printer-type=0x2900c</txt-record>
<txt-record>pdl=application/octet-stream,application/pdf,application/postscript,application/vnd.cups-raster,image/gif,image/jpeg,image/png,image/tiff,image/urf,text/html,text/plain,application/vnd.adobe-reader-postscript,application/vnd.cups-pdf</txt-record>
  </service>
</service-group>

Save and close the file. Restart Avahi-daemon.

sudo systemctl restart avahi-daemon

Now iOS and macOS clients in the same network should be able to use your printer. The following screenshot shows my iPhone successfully found an AirPrint Printer.

服务器/nas/cups.txt · 最后更改: 2020/09/25 21:03 由 caiweizhi