1,747   Ubuntu

Ubuntu 提取 root 权限使用 sudo,但是在执行 echo x > test.txt命令时却不生效,报错如下


chenyunhui@ubuntu:/var/log/redis$ sudo echo 511 > /proc/sys/net/core/somaxconn
bash: /proc/sys/net/core/somaxconn: Permission denied

这是因为 sudo 只使 echo 有了 root 的权限,但是 > 符号还是没有 root 权限

解决方法:
使用tee管道,该命令可以从标准输入中读入信息并将其写入标准输出或文件中

// 等同于 echo x > test.txt
echo x |sudo tee test.txt
 // -a 是追加,等同于 echo x >> test.txt
echo x |sudo tee -a test.txt



Leave a Reply

Your email address will not be published. Required fields are marked *