我读了文档:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandboxing.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandbox_ipc.md
但无法弄清楚如何正确配置沙箱,并且无法在我的系统上找到脚本update-linux-sandbox.sh
。
我发现它here
但我得到:
$ ./update-linux-sandbox.sh
/tmp/../out/Debug does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
$ BUILDTYPE=Release ./update-linux-sandbox.sh
/tmp/../out/Release does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
我唯一不安全的解决方法是使用:
const browser = await puppeteer.launch(
{headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']}
);
有没有想过做正确的事情?
如果你正在寻找一种在没有--no-sandbox
arg的情况下在Centos7中运行Puppeteer的方法,那么@MevatlaveKraspek答案将无效
我设法通过设置Linux内核参数来启用命名空间(在CentOS Linux版本7.4.1708上),让Puppeteer在没有--no-sandbox
flag arg的情况下获取屏幕截图。
以root用户身份运行:echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
检查它是否适用于:sudo sysctl -a | grep user.max_user_namespaces
现在重新启动系统并运行脚本而不使用--no-sandbox
,例如const browser = await puppeteer.launch();
如果它仍然不起作用,您可能正在使用较旧的Linux内核,并且需要在内核中设置几个额外的args。
以root用户身份运行:
grubby --args="user_namespace.enable=1 namespace.unpriv_enable=1" --update-kernel="$(grubby --default-kernel)"
现在重新启动系统并检查刚刚添加的2个参数的内核命令行
cat /proc/cmdline
如果他们在命令行中运行脚本而不再使用--no-sandbox
,例如const browser = await puppeteer.launch();
它现在应该工作。如果不是,您可能正在使用不支持命名空间的旧内核。
您可以通过以下方式检查您的内核版本:uname -a
这是我的内核版本,我已经让Puppeteer在没有--no-sandbox
arg的情况下运行。 Linux centos7 3.10.0-693.21.1.el7.x86_64
希望这可以帮助 :)
对于Debian,在我的版本9 Stretch中,问题似乎与沙盒没有打开有关。 Chromium将吐出致命信息:
对于直到重新启动的解决方案(以root身份从命令行运行):
对于更永久的解决方案(以root身份从命令行运行):
更多Debian相关信息可以在这里找到:
想出来:来自内核的enable user namespace cloning:
sudo sysctl -w kernel.unprivileged_userns_clone=1
你可以试试:
以root用户身份运行:echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
重新加载sysctl:sysctl -p