简体   繁体   English

使用Java程序如何以非root用户身份以编程方式在端口80上启动Jetty

[英]Using Java Program How to programmatically start Jetty on port 80 as non-root user

I'm trying following progam: 我正在尝试以下程序:

  1. If i use SUDO java to run everthing is fine, but I don't want to use SUDO 如果我使用SUDO java运行everthing很好,但我不想使用SUDO
  2. without SUDO i get following error: SocketException: Permission Denied (as its PORT 80) 没有SUDO我得到以下错误: SocketException: Permission Denied (as its PORT 80)

Using jetty documentation, I get this to work using command line where i change 使用jetty文档,我使用命令行工作,我改变

  1. jetty-setuid.xm l -- Put user-name is non-root user jetty-setuid.xm l - Put用户名是非root用户
  2. start.ini -- Change to EXEC and passing etc/jetty-setuid.xml as first parameter start.ini - 更改为EXEC并将etc/jetty-setuid.xml作为第一个参数传递
  3. jetty.xml -- To have port number as 80 jetty.xml - 将端口号设置为80

then I still do sudo as non-root user -- like -> sudo java -jar start.jar Jetty starts on port 80 as non-root user. 然后我仍然以非root用户身份执行sudo - 比如 - > sudo java -jar start.jar Jetty作为非root用户在端口80上启动。

I want to ACHIEVE the same using JAVA Program. 我想用JAVA程序实现同样的目标。 Any help/comments are appreciated. 任何帮助/意见表示赞赏。

package my.package;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

public class JettyTest {


public static void main(String[] args) throws Exception
{
    Server server = new Server();
    System.out.println("Created new server, now going to start");
    SelectChannelConnector connector0 = new SelectChannelConnector();
    connector0.setPort(80); //on port 80
    connector0.setMaxIdleTime(30000);
    connector0.setRequestHeaderSize(8192);

    server.setConnectors(new Connector[]{ connector0 });

    server.setHandler(new MyHandler()); //simple hello world handler

    server.start();
    System.out.println("started server on port 80");
    server.join();
}

}

You must use sudo somehow on Unix to elevate to root priviledges. 你必须在Unix上以某种方式使用sudo来升级到root权限。 Otherwise you cannot get port 80. 否则你无法获得端口80。

For Java programs, the sudo must be applied to the java command itself, but it is generally a bad idea to do that. 对于Java程序,sudo必须应用于java命令本身,但这通常是一个坏主意。

A more conservative solution is to bind to another port, say 8080, and then reroute port 80 to that port. 更保守的解决方案是绑定到另一个端口,比如8080,然后将端口80重新路由到该端口。 The commands to do so varies wildly between operating systems and may not even exist on some older Unix-versions. 执行此操作的命令在操作系统之间变化很大,甚至可能在某些较旧的Unix版本中也不存在。

Sudo elevates the privileges of your command to root level. Sudo将命令的权限提升到根级别。 The reason why you need to "sudo" here (or, more generally speaking, run the command as root) is the fact that in Linux the ports below 1024 are reserved to root. 您需要在这里“sudo”(或者更一般地说,以root身份运行命令)的原因是,在Linux中,1024以下的端口保留给root。

In other words - you cannot run jetty (or any other service) on port 80 if you're not root. 换句话说 - 如果你不是root用户,你不能在端口80上运行jetty(或任何其他服务)。

The simplest solution here is to run Jetty on another port (8080 is a common one). 这里最简单的解决方案是在另一个端口上运行Jetty(8080是常见端口)。

If you still want users to access the service on the default port, consult http://wiki.eclipse.org/Jetty/Howto/Port80 and http://docs.codehaus.org/display/JETTY/port80 . 如果您仍希望用户访问默认端口上的服务,请访问http://wiki.eclipse.org/Jetty/Howto/Port80http://docs.codehaus.org/display/JETTY/port80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM