You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何树莓派PostScript RIP打印服务器配置脚本生效异常?

树莓派PostScript RIP打印服务器IPP连接问题排查与修复

问题描述

尝试将树莓派配置为PostScript RIP打印服务器,执行setup-rip-server.sh脚本后显示成功,但Mac端无法连接IPP服务,提示“无法连接‘ipp’,是否仍要创建打印机?”。

脚本内容

#!/bin/bash
# setup-rip-server.sh — Configure Raspberry Pi as a PostScript RIP print server
# Usage: sudo ./setup-rip-server.sh [-v]

set -e

# Check for root privileges
if [ "$EUID" -ne 0 ]; then
    echo "This script must be run as root."
    echo "Please re-run it using: sudo ./setup-rip-server.sh"
    exit 1
fi

# Verbose mode flag
VERBOSE=false
if [ "$1" == "-v" ]; then
    VERBOSE=true
fi

# Output control
quiet() {
    if $VERBOSE; then
        "$@"
    else
        "$@" &>/dev/null
    fi
}

REQUIRED_PACKAGES=(cups cups-bsd ghostscript cron)

echo "Checking required packages..."
for pkg in "${REQUIRED_PACKAGES[@]}"; do
    if dpkg -s "$pkg" &>/dev/null; then
        echo "$pkg is already installed."
    else
        echo "Installing $pkg..."
        quiet apt update
        quiet apt install -y "$pkg"
    fi
done

echo "Checking CUPS service status..."
if systemctl is-enabled cups &>/dev/null; then
    echo "CUPS is already enabled."
else
    echo "Enabling CUPS..."
    quiet systemctl enable cups
fi

if systemctl is-active cups &>/dev/null; then
    echo "CUPS is already running."
else
    echo "Starting CUPS..."
    quiet systemctl start cups
fi

echo "Configuring CUPS for remote access..."
CUPSD_CONF="/etc/cups/cupsd.conf"
cp "$CUPSD_CONF" "$CUPSD_CONF.bak"

sed -i 's/^Listen localhost:631$/Port 631/' "$CUPSD_CONF"
sed -i '/^<Location \/>/,/^<\/Location>/ s/Require local/Allow all/' "$CUPSD_CONF"
sed -i '/^<Location \/admin>/, /^<\/Location>/ s/Require local/Allow all/' "$CUPSD_CONF"
sed -i '/^<Location \/admin\/conf>/, /^<\/Location>/ s/Require local/Allow all/' "$CUPSD_CONF"

echo "Restarting CUPS with updated configuration..."
quiet systemctl restart cups

echo "Discovering available printers..."
PRINTERS=($(lpstat -p | awk '{print $2}'))

if [ ${#PRINTERS[@]} -eq 0 ]; then
    echo "No printers found. Please add a printer via http://<raspberrypi.local>:631"
    exit 1
fi

echo "Available printers:"
for i in "${!PRINTERS[@]}"; do
    echo "$((i+1)). ${PRINTERS[$i]}"
done

read -p "Enter the number of the printer to set as default: " SELECTION

if ! [[ "$SELECTION" =~ ^[0-9]+$ ]] || [ "$SELECTION" -lt 1 ] || [ "$SELECTION" -gt "${#PRINTERS[@]}" ]; then
    echo "Invalid selection. No changes made."
    exit 1
fi

DEFAULT_PRINTER="${PRINTERS[$((SELECTION-1))]}"
quiet lpoptions -d "$DEFAULT_PRINTER"
echo "Default printer set to: $DEFAULT_PRINTER"

PURGE_SCRIPT="/usr/local/bin/purge-cups-queue.sh"
echo "Installing nightly queue purge script at $PURGE_SCRIPT..."

cat <<EOF > "$PURGE_SCRIPT"
#!/bin/bash
# purge-cups-queue.sh — Clears all CUPS print queues nightly

cancel -a
echo "$(date): CUPS print queue purged." >> /var/log/cups-purge.log
EOF

chmod +x "$PURGE_SCRIPT"

echo "Scheduling daily purge via cron..."
CRON_ENTRY="0 0 * * * $PURGE_SCRIPT"
(crontab -l 2>/dev/null | grep -F "$PURGE_SCRIPT") || (crontab -l 2>/dev/null; echo "$CRON_ENTRY") | crontab -

echo "RIP print server setup complete."
echo "Clients can now send PostScript jobs via IPP to this Raspberry Pi."
echo "Print queue will be automatically purged nightly at midnight."

当前cupsd.conf配置

LogLevel warn
MaxLogSize 0
# Allow remote access
Port 631
Listen /var/run/cups/cups.sock
Listen 0.0.0.0:631
Browsing On
BrowseLocalProtocols dnssd
DefaultAuthType Basic
WebInterface Yes
<Location />
  # Allow remote access...
  Order allow,deny
  Allow all
</Location>
<Location /admin>
</Location>
<Location /admin/conf>
  AuthType Default
  Require user @SYSTEM
</Location>
<Policy default>
  JobPrivateAccess default
  JobPrivateValues default
  SubscriptionPrivateAccess default
  SubscriptionPrivateValues default
  <Limit Create-Job Print-Job Print-URI Validate-Job>
    Order deny,allow
  </Limit>
  <Limit Send-Document Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs Set-Job-Attributes Create-Job-Subscription Renew-Subscription Cancel-Subscription Get-Notifications Reprocess-Job Cancel-Current-Job Suspend-Current-Job Resume-Job Cancel-My-Jobs Close-Job CUPS-Move-Job CUPS-Get-Document>
    Require user @OWNER @SYSTEM
    Order deny,allow
  </Limit>
  <Limit CUPS-Add-Modify-Printer CUPS-Delete-Printer CUPS-Add-Modify-Class CUPS-Delete-Class CUPS-Set-Default CUPS-Get-Devices>
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  </Limit>
  <Limit Pause-Printer Resume-Printer Enable-Printer Disable-Printer Pause-Printer-After-Current-Job Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer Activate-Printer Restart-Printer Shutdown-Printer Startup-Printer Promote-Job Schedule-Job-After Cancel-Jobs CUPS-Accept-Jobs CUPS-Reject-Jobs>
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  </Limit>
  <Limit Cancel-Job CUPS-Authenticate-Job>
    Require user @OWNER @SYSTEM
    Order deny,allow
  </Limit>
  <Limit All>
    Order deny,allow
  </Limit>
</Policy>
<Policy authenticated>
  JobPrivateAccess default
  JobPrivateValues default
  SubscriptionPrivateAccess default
  SubscriptionPrivateValues default
  <Limit Create-Job Print-Job Print-URI Validate-Job>
    AuthType Default
    Order deny,allow
  </Limit>
  <Limit Send-Document Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs Set-Job-Attributes Create-Job-Subscription Renew-Subscription Cancel-Subscription Get-Notifications Reprocess-Job Cancel-Current-Job Suspend-Current-Job Resume-Job Cancel-My-Jobs Close-Job CUPS-Move-Job CUPS-Get-Document>
    AuthType Default
    Require user @OWNER @SYSTEM
    Order deny,allow
  </Limit>
  <Limit CUPS-Add-Modify-Printer CUPS-Delete-Printer CUPS-Add-Modify-Class CUPS-Delete-Class CUPS-Set-Default>
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  </Limit>
  <Limit Pause-Printer Resume-Printer Enable-Printer Disable-Printer Pause-Printer-After-Current-Job Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer Activate-Printer Restart-Printer Shutdown-Printer Startup-Printer Promote-Job Schedule-Job-After Cancel-Jobs CUPS-Accept-Jobs CUPS-Reject-Jobs>
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  </Limit>
  <Limit Cancel-Job CUPS-Authenticate-Job>
    AuthType Default
    Require user @OWNER @SYSTEM
    Order deny,allow
  </Limit>
  <Limit All>
    Order deny,allow
  </Limit>
</Policy>

排查与修复步骤

1. 验证网络连通性

  • 在Mac上执行ping raspberrypi.local,确认能正常连通;如果不通,安装树莓派Bonjour服务:sudo apt install avahi-daemon,安装完成后重启服务sudo systemctl restart avahi-daemon。
  • 在Mac上执行nc -zv raspberrypi.local 631,确认631端口可访问;如果连接失败,临时关闭树莓派防火墙测试:sudo iptables -F,测试通过后永久开放端口:sudo ufw allow 631(若使用ufw防火墙)。

2. 修正CUPS配置文件

从当前配置看,<Location /admin>块缺失关键配置,按以下方式修改:

  • 编辑/etc/cups/cupsd.conf,更新<Location /admin>块:
<Location /admin>
  Order allow,deny
  Allow all
  AuthType Default
  Require user @SYSTEM
</Location>
  • 确保普通打印权限允许远程访问,在<Policy default>的<Limit Create-Job Print-Job Print-URI Validate-Job>块内添加允许规则:
<Limit Create-Job Print-Job Print-URI Validate-Job>
  Order deny,allow
  Allow all
</Limit>
  • 重启CUPS服务:sudo systemctl restart cups

3. 确认IPP路径正确性

Mac端添加打印机时,需使用完整IPP路径,格式为:
ipp://raspberrypi.local:631/printers/<你的打印机名称>
可通过访问树莓派CUPS管理页面http://raspberrypi.local:631/printers查看打印机的准确路径。

4. Mac端配置调整

  • 手动添加打印机:打开系统设置-打印机与扫描仪,点击+号,选择IP标签,协议选Internet Printing Protocol (IPP),地址填raspberrypi.local,端口631,队列填printers/<打印机名称>,选择对应PostScript驱动完成添加。
  • 清除Mac打印缓存:执行sudo rm -rf /Library/Printers/,重启Mac后重新添加打印机。

5. 查看CUPS日志定位问题

在树莓派上实时查看CUPS错误日志:tail -f /var/log/cups/error_log,同时在Mac端尝试连接,根据日志中的错误信息(如权限拒绝、路径不存在等)精准排查问题。

内容的提问来源于stack exchange,提问作者headbanger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:15:54