在 CentOS 下,nohup 默认会把输出写到 当前目录的 nohup.out,权限问题通常体现在 文件权限 / 目录权限 / SELinux 三个方面。下面按常见场景给你说明。
nohup.out 的权限ls -l nohup.out
chmod 644 nohup.out
或允许所有用户读写(不推荐生产环境):
chmod 666 nohup.out
chown user:group nohup.out
nohup.out 所在目录的权限(重点)⚠️ 如果目录没写权限,nohup 会失败或无法创建日志
ls -ld .
如果你是在某个目录下执行 nohup:
chmod u+w /path/to/dir
或:
chown user:group /path/to/dir
nohup 日志文件并控制权限(推荐做法 ✅)不要直接用默认的 nohup.out,自己指定日志文件更可控:
nohup your_command > /var/log/myprogram.log 2>&1 &
然后设置权限:
touch /var/log/myprogram.log
chown user:user /var/log/myprogram.log
chmod 644 /var/log/myprogram.log
getenforce
如果是 Enforcing,可能是 SELinux 阻止写日志。
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
给日志目录设置正确的 SELinux context:
semanage fcontext -a -t var_log_t "/var/log/myprogram.log"
restorecon -v /var/log/myprogram.log
nohup: failed to run command 'xxx': Permission denied
✅ 解决:
chmod +x your_command
检查:
df -h # 磁盘是否满
ls -ld . # 目录是否可写
✅ 最规范做法
nohup your_command > /var/log/myprogram.log 2>&1 &
chown appuser:appuser /var/log/myprogram.log
chmod 644 /var/log/myprogram.log
如果你愿意,可以把:
nohup 启动命令贴出来,我可以帮你精准定位是哪一层权限问题。