[英]Ruby on Rails: permission denied when using “rails generate controller welcome”
I used Ruby on Rails on Red Hat server.我在 Red Hat 服务器上使用了 Ruby on Rails。 When I trying to generate a controller file, I got this error:
当我尝试生成控制器文件时,出现此错误:
[ec2-user@ip-172-31-22-128 testApp4]$ rails generate controller welcome
/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `initialize': Permission denied @ rb_sysopen - /tmp/spring/fea371aaf9d69cfa58bd12f69b3f1bf6.pid (Errno::EACCES)
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `open'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `open'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in `initialize'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:14:in `new'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:14:in `boot'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:43:in `block in boot_server'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:41:in `fork'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:41:in `boot_server'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:24:in `call'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/rails.rb:23:in `call'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
from /home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
from /home/ec2-user/testApp4/bin/spring:16:in `require'
from /home/ec2-user/testApp4/bin/spring:16:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
I solved that problem by running我通过运行解决了这个问题
sudo chmod -R 1777 /tmp
hope this helps other people like me who would prefer not having to deactivate the spring gem希望这能帮助像我这样不想停用 spring gem 的其他人
It needs ownership to write the re-write the pid for each server start.它需要所有权来为每个服务器启动重写 pid。
I had to run it with my full local path & sudo我必须使用完整的本地路径和 sudo 运行它
$ sudo chmod -R 777 /Users/MyName/Desktop/projects/my_project/tmp/ $ sudo chmod -R 777 /Users/MyName/Desktop/projects/my_project/tmp/
I took a look at the library that's trying to write the pid file, lib/spring/env.rb
.我查看了试图编写 pid 文件
lib/spring/env.rb
。
The function in question tries to create a temporary directory at the same location each time unless the XDG_RUNTIME_DIR
is set:除非设置了
XDG_RUNTIME_DIR
否则该XDG_RUNTIME_DIR
每次都会尝试在同一位置创建一个临时目录:
path = Pathname.new(File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, "spring"))
Setting said variable to a unique directory does the trick for me:将所述变量设置为一个唯一的目录对我来说很有用:
export XDG_RUNTIME_DIR=/tmp/`whoami`
Missing permission for tmp folder to writable .缺少 tmp 文件夹的 writable 权限。 run
chmod 777 tmp/
运行
chmod 777 tmp/
I had this same issue when working on a Rails 6 application in Ubuntu 20.04.我在 Ubuntu 20.04 中处理 Rails 6 应用程序时遇到了同样的问题。
Each time I run the command rails assets:clobber
to remove the old assets in public/assets
completely, I get the error:每次我运行命令
rails assets:clobber
以完全删除public/assets
中的旧资产时,我都会收到错误消息:
errno::enotempty: directory not empty @ dir_s_rmdir
errno::enotempty: 目录不为空@dir_s_rmdir
errno::eacces: permission denied @ apply2files
errno::eacces: 权限被拒绝 @ apply2files
And the logs pointed the path to the file causing it, which in my a case was the public/packs/manifest.json
file.并且日志指向导致它的文件的路径,在我的情况下是
public/packs/manifest.json
文件。
I tried deleting it or changing permission but nothing worked.我尝试删除它或更改权限,但没有任何效果。 I also tried deleting the
tmp
directory in the project, but it did not work.我也试过删除项目中的
tmp
目录,但是没有用。
Here's how I fixed it :这是我修复它的方法:
I listed all the files and directories in that directory using the ls -lh
command, which gave me this output:我使用
ls -lh
命令列出了该目录中的所有文件和目录,该命令为我提供了以下输出:
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 1.7K Oct 6 20:29 404.html
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 1.7K Oct 6 20:29 422.html
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 1.6K Oct 6 20:29 500.html
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 0 Oct 6 20:29 apple-touch-icon.png
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 0 Oct 6 20:29 apple-touch-icon-precomposed.png
drwxr-xr-x 3 promisechukwuenyem promisechukwuenyem 4.0K Oct 13 09:20 armstrong_tools
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 0 Oct 6 20:29 favicon.ico
drwxr-xr-x 3 root root 4.0K Oct 8 13:06 packs
-rw-rw-r-- 1 promisechukwuenyem promisechukwuenyem 99 Oct 6 20:29 robots.txt
I realized that the permissions for the files and directories were fine, however, the packs
directory had root
ownership which was seperate from persmission of other files and directories:我意识到文件和目录的权限很好,但是,
packs
目录具有与其他文件和目录的权限分开的root
所有权:
drwxr-xr-x 3 root root 4.0K Oct 8 13:06 packs
I simply ran the command below to change the ownership from root to promisechukwuenyem:我只是运行下面的命令将所有权从 root 更改为 promisechukwuenyem:
sudo chown -R promisechukwuenyem:promisechukwuenyem packs/
Now when I ran the command rails assets:clobber
to remove the old assets in public/assets
completely, it worked just fine.现在,当我运行 command
rails assets:clobber
以完全删除public/assets
中的旧资产时,它工作得很好。
That's all.就这样。
I hope this helps我希望这有帮助
I am getting this type of error(permission denied @ apply2files) when using rails server
.使用
rails server
时出现此类错误(permission denied @ apply2files)。
sudo rails server
solve my problem. sudo rails server
解决了我的问题。
Or,或者,
export XDG_RUNTIME_DIR=/run/user/${id -u}
to change this system variable.更改此系统变量。
id -u
returns your UID(user id), which is the dir name where you have access to write/read under /run/user/
. id -u
返回您的 UID(用户 ID),这是您有权在/run/user/
下写入/读取的目录名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.