Tuesday, September 27, 2011

Minecraft server and IPv6

I was having trouble starting a Minecraft server in Ubuntu Natty. I could connect locally, but my friend couldn't connect over the internet.

I used the standard command to start the server.

$ java -Xmx1024M -Xms1024M \
-jar minecraft_server.jar nogui

I noticed that the server was listening on an IPv6 address. My local network and internet connection is IPv4-only, so my friend would obviously be unable to connect.

$ netstat -an | grep 25565
tcp6  0  0  :::25565  :::*  LISTEN

The solution was to pass a property to Java telling it to bind to an IPv4 address.

$ java -Xmx1024M -Xms1024M -Djava.net.preferIPv4Stack=true \
-jar minecraft_server.jar nogui

With that option the server will listen on an IPv4 address.

$ netstat -an | grep 25565
tcp  0  0  0.0.0.0:25565  0.0.0.0:*  LISTEN

10 comments:

  1. Awesome! Fixed the problem precisely. Thanks for the write-up!

    ReplyDelete
  2. Awesome, thank you for sharing.. over a year later and still relevant!

    ReplyDelete
  3. What if you needed the server to have both IPv4 and IPv6 addresses, because you had players from both Internets?

    ReplyDelete
  4. @Chris - I don't know if Minecraft supports binding to multiple addresses. Let's assume it doesn't, which means you can only run your server on either an IPv4 or IPv6 address.

    If your server is behind a firewall, then you could probably use port forwarding to do what you want. Run the server on one address internally, then set up two port forwards from your firewall to your server's address. One port forward would come from your IPv6 address and one would come from your IPv4 address.

    If your server is a server directly on the internet then you will still run Minecraft on one address (e.g. IPv4). Then what you may be able to do is set up a tunnel from your other address (e.g. IPv6) to that address. That will make it look like your server has two addresses. Here's an example of how to set up a tunnel using a tool called socat: https://ef.gy/forwarding-ipv4-to-ipv6

    ReplyDelete
  5. Thanks dude, this was exactly what was needed.

    ReplyDelete
  6. can confirm this is still helpful in 2017 :D

    ReplyDelete
  7. Glad to hear it's still relevant :)

    ReplyDelete
  8. Netstat still shows as listening on tcp6, although my output looks a little different than yours:

    tcp6 0 0 0.0.0.0:25565 [::]:* LISTEN

    It looks like the local address is presented as IPv4, but the foreign address is in IPv6? Any help would be appreciated.

    ReplyDelete
  9. Hi @Nixx, I'm not sure about that format, is it an IPv4-mapped IPv6 address?

    https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses

    ReplyDelete