Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening port 80 with Java application on Ubuntu

Tags:

java

port

ubuntu

What I need to do is running a Java application which is a RESTful service server side writtern by Restlet. And this service will be called by another app running on Google App Engine.

Because of the restriction of GAE, every http call is limited to port 80 and 443 (http and https) with HttpUrlConnection class. As a result, I have to deploy my server side application on port 80 or 443.

However, because the app is running on Ubuntu, and those ports under 1024 cannot be accessed by non-root user, then a Access Denied exception will be thrown when I run my app.

The solutions that have come into my mind includes:

  1. Changing the security policy of JRE, which is the files resides in /lib/security/java.policy, to grantjava.net.SocketPermission "*.80" "listen, connect, accept, resolve" permission。However, neither using command line to include this file or overrides the content in JRE's java.policy file, the same exception keeps coming out.

  2. try to login as a root user, however because my unfamiliarity with Unix, I don't know how to do it.

  3. another solution I haven't try is to map all calls to 80 to a higher port like 1234, then I can deploy my app on 1234 without problem, and GAE call send request to port 80. But how to connect the missing gap is still a problem.

Currently I am using a "hacking" method, which is to package the application into a jar file, and sudo running the jar file with root privilege. It works now, but definitely not appropriate in the real deployment environment.

So if anyone have any idea about the solution, thanks very much!

like image 893
Yudong Li Avatar asked Mar 15 '10 01:03

Yudong Li


People also ask

Does Ubuntu open port 80?

By default, the port 80 for http connection and port 443 for https is blocked on Ubuntu 22.04 as you can only access this port from the actual localhost and not from any other public host. To open ports 80 and 443 we need to add an iptables rule.

How do I open port 80 on Linux?

For IPv6 based firewall you need to edit the /etc/sysconfig/ip6tables file. Open this file using a text editor such as vi/vim or emacs or nano text edior and then edit or append as per your need to open TCP port 80.


2 Answers

You can use iptables to redirect using something like this:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport http -j REDIRECT --to-ports 8080

Make the changes permanent (persist after reboot) with:

iptables-save
like image 89
objects Avatar answered Sep 21 '22 11:09

objects


Solution 1: It won't change anything, this is not a Java limitation, it's the OS that is preventing you to use privileged port numbers (ports lower than 1024).

Solution 2: Not a good idea IMO, there are good reasons to not run a process as root.

Solution 3: Use setcap or iptables. See this previous question.

like image 41
Pascal Thivent Avatar answered Sep 17 '22 11:09

Pascal Thivent