Jetty Logo
Version: 9.2.2-SNAPSHOT
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Setting Port 80 Access for a Non-Root User

Using ipchains
Using iptables
Configuring Jetty's SetUID Feature
Using xinetd
Using the Solaris 10 User Rights Management Framework

On Unix-based systems, port 80 is protected; typically only the superuser root can open it. For security reasons, it is not desirable to run the server as root. This page presents several options to access port 80 as a non-root user, including using ipchains, iptables, Jetty's SetUID feature, xinetd, and the Solaris 10 User Rights Management Framework.

Using ipchains

On some Linux systems you can use the ipchains REDIRECT mechanism to redirect from one port to another inside the kernel (if ipchains is not available, then usually iptables is):

# /sbin/ipchains -I input --proto TCP --dport 80 -j REDIRECT 8080

This command instructs the system as follows: "Insert into the kernel's packet filtering the following as the first rule to check on incoming packets: if the protocol is TCP and the destination port is 80, redirect the packet to port 8080". Be aware that your kernel must be compiled with support for ipchains (virtually all stock kernels are). You must also have the ipchains command-line utility installed. You can run this command at any time, preferably just once, since it inserts another copy of the rule every time you run it.

Using iptables

On many Linux systems you can use the iptables REDIRECT mechanism to redirect from one port to another inside the kernel (if iptables is not available, then usually ipchains is).

You need to add something like the following to the startup scripts or your firewall rules:

# /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

The underlying model of iptables is different from ipchains, so the forwarding normally happens only to packets originating off-box. You also need to allow incoming packets to port 8080 if you use iptables as a local firewall.

Be careful to place rules like this one early in your input chain. Such rules must precede any rule that accepts the packet, otherwise the redirection won't occur. You can insert as many rules as required if your server needs to listen on multiple ports, as for HTTPS.

Configuring Jetty's SetUID Feature

SetUID is a technique that uses Unix-like file system access right to allow users to run an executable that would otherwise require higher privileges.

Jetty's SetUID module allows you to run Jetty as a normal user even when you need to run Jetty on port 80 or 443.

To use it with the jetty distribution:

  1. Ensure that you have the http.mod (and https.mod if you are using SSL) modules enabled for the base you are using. The http.mod is enabled by default in the distribution, while the https.mod is only enabled in the demo-base directory.

  2. Ensure that you have changed the http port to 80 (and changed the https port to 443 if you are using SSL).

  3. Enable the setuid.mod module:

    # java -jar start.jar --add-to-start=setuid

    Note

    The --add-to-start command will enable the setuid module for this and all subsequent executions of jetty. There are other ways to enable the module, such as just for this execution. For more information on the alternatives see the section on Managing Startup Modules.

  4. Edit the configuration for the setuid module to substitute the userid and groupid of the user to switch to after starting. If you used the --add-to-start command, this configuration is in the start.ini file. If you used the --add-to-startd command instead, this configuration is in the start.d/setuid.ini file instead. Here are the lines to configure:

    Note

    As well as opening the connectors as root, you can also have jetty start the Server as root before changing to the non-root user.

  5. You now need a native code library to do the user switching. This code is hosted as part of the Jetty ToolChain project and it is released independently from Jetty itself. You can find the source code here in the jetty-setuid project. Build it locally, which will produce a native library appropriate for the operating system:

    # mvn clean install

    If you built on a linux machine you will find the native library in jetty-setuid/libsetuid-linux/target/libsetuid-linux.so. If you built on a different operating system you will find the library in a different subdirectory, with the name containing the name of the operating system. You might like to copy this file into your jetty distribution's lib directory.

  6. Start jetty as the root user in your base directory, providing the location of the native library to java. Here's an example of how to do it on the command line, assuming were are in the demo-base directory:

    # sudo java -Djava.library.path=libsetuid-linux -jar ../start.jar

Using xinetd

With modern Linux flavours, inetd has a newer, better big brother xinetd, that you can use to redirect network traffic. Since xinetd is driven by text files, all you need is a text editor. For detailed information, see http://linux.die.net/man/5/xinetd.conf.

There are two ways to give xinetd instructions:

  • Add a new service to etc/xinetd.conf

  • Add a new file to the directory etc/xinetd.d

The format is the same; if you have a look at the file/directory, you will get the picture.

The following entry redirects all inward TCP traffic on port 80 to port 8888 on the local machine. You can also redirect to other machines for gimp proxying:

service my_redirector
{
 type = UNLISTED
 disable = no
 socket_type = stream
 protocol = tcp
 user = root
 wait = no
 port = 80
 redirect = 127.0.0.1 8888
 log_type = FILE /tmp/somefile.log
}

caveats

Be aware of the following:

  • Include a space on either side of the '=' or it is ignored.

  • type = UNLISTED means that the name of the service does not have to be listed in /etc/services, but then you have to specify port and protocol. If you want to do use an existing service name, for example, http:

    service http
    {
     disable = no
     socket_type = stream
     user = root
     wait = no
     redirect = 127.0.0.1 8888
     log_type = FILE /tmp/somefile.log
    }

    Have a browse in /etc/services and it will all become clear.

  • Logging might present certain security problems, so you might want to leave that out.

xinetd is a hugely powerful and configurable system, so expect to do some reading.

Using the Solaris 10 User Rights Management Framework

Solaris 10 provides a User Rights Management framework that can permit users and processes superuser-like abilities:

usermod -K defaultpriv=basic,net_privaddr myself

Now the myself user can bind to port 80.

Refer to the Solaris 10 and Solaris 11 Security Services documentation for more information.

See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-08-20T01:00:22-07:00)