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

Linux环境下Rails服务器无法启动问题求助

Fixing "getaddrinfo: Name or service not known" When Starting Rails Server

Hey there, let's tackle this Rails server error you're hitting. That SocketError about getaddrinfo means Ruby can't resolve the hostname (usually localhost) to an IP address, so WEBrick can't start listening for connections. Here's how to fix it:

First, Try a Quick Workaround

Start the server by explicitly binding to the loopback IP instead of relying on localhost resolution—this should get things running right away:

rails server -b 127.0.0.1

Fix the Root Issue: Check Your Hosts File

On Linux, the /etc/hosts file maps hostnames to IP addresses. If localhost isn't properly mapped here, you'll get this error every time.

  1. Open the hosts file with sudo privileges (you need admin access to edit it):
    sudo nano /etc/hosts
    
  2. Make sure these lines are present (add them if they're missing):
    127.0.0.1   localhost
    ::1         localhost
    
  3. Save the file (in nano, press Ctrl+O, hit Enter, then Ctrl+X to exit).
  4. Restart your Rails server normally with rails server—it should work now.

Check for Misconfigured HOST Environment Variable

If the above steps don't resolve the issue, double-check that you haven't set an invalid HOST environment variable. Run this command to check:

echo $HOST

If it returns something other than localhost or 127.0.0.1, unset it with:

unset HOST

Then try starting the server again.

Your Original Error Output

For reference, here's the full error you encountered:

=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2018-05-28 15:55:12] INFO  WEBrick 1.3.1
[2018-05-28 15:55:12] INFO  ruby 2.3.1 (2016-04-26) [x86_64-linux-gnu]
Exiting
/usr/lib/ruby/2.3.0/socket.rb:231:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
	from /usr/lib/ruby/2.3.0/socket.rb:231:in `foreach'
	from /usr/lib/ruby/2.3.0/socket.rb:757:in `tcp_server_sockets'
	from /usr/lib/ruby/2.3.0/webrick/utils.rb:65:in `create_listeners'
	from /usr/lib/ruby/2.3.0/webrick/server.rb:134:in `listen'
	from /usr/lib/ruby/2.3.0/webrick/server.rb:115:in `initialize'
	from /usr/lib/ruby/2.3.0/webrick/httpserver.rb:47:in `initialize'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/rack-1.6.10/lib/rack/handler/webrick.rb:31:in `new'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/rack-1.6.10/lib/rack/handler/webrick.rb:31:in `run'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/rack-1.6.10/lib/rack/server.rb:287:in `start'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands/server.rb:80:in `start'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
	from /home/jack/Desktop/Rails/MegsBlog/bin/rails:9:in `require'
	from /home/jack/Desktop/Rails/MegsBlog/bin/rails:9:in `<top (required)>'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
	from /home/jack/Desktop/Rails/MegsBlog/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
	from /home/jack/Desktop/Rails/MegsBlog/bin/spring:15:in `<top (required)>'
	from bin/rails:3:in `load'
	from bin/rails:3:in `<main>'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:45:41