Ubuntu 运行tomcat报错:java.net.SocketException: Permission denied

在Ubuntu系统中,使用非root用户开启tomcat时报错:

 java.net.SocketException: Permission denied

at sun.nio.ch.Net.bind0(Native Method)

at sun.nio.ch.Net.bind(Net.java:433)

at sun.nio.ch.Net.bind(Net.java:425)

at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)

at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)

at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210)

at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:982)

at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:245)

at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:620)

at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:66)

at org.apache.catalina.connector.Connector.initInternal(Connector.java:997)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

at org.apache.catalina.core.StandardService.initInternal(StandardService.java:549)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:875)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

at org.apache.catalina.startup.Catalina.load(Catalina.java:621)

at org.apache.catalina.startup.Catalina.load(Catalina.java:644)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:311)

at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:494)

14-Jan-2018 22:30:57.969 SEVERE [main] org.apache.catalina.core.StandardService.initInternal Failed to initialize connector [Connector[HTTP/1.1-443]]

 org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-443]]

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:113)

at org.apache.catalina.core.StandardService.initInternal(StandardService.java:549)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:875)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

at org.apache.catalina.startup.Catalina.load(Catalina.java:621)

at org.apache.catalina.startup.Catalina.load(Catalina.java:644)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:311)

at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:494)

Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed

at org.apache.catalina.connector.Connector.initInternal(Connector.java:1000)

at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)

... 12 more

Caused by: java.net.SocketException: Permission denied

at sun.nio.ch.Net.bind0(Native Method)

at sun.nio.ch.Net.bind(Net.java:433)

at sun.nio.ch.Net.bind(Net.java:425)

at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)

at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)

at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210)

at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:982)

at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:245)

at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:620)

at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:66)

at org.apache.catalina.connector.Connector.initInternal(Connector.java:997)

... 13 more

原因:Ubuntu系统中,非root用户只能使用1024以上的端口。

两个解决办法:

  1. 使用root用户开启tomcat
  2. 将端口设置为1024以上的端口:
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" URIEncoding="UTF-8"                     maxThreads="150" scheme="https" secure="true"
                     keystoreFile=""
                     keystorePass=""
                     clientAuth="false" sslProtocol="TLS" />
     将端口由443修改为8443,tomcat即可正常运行。

     但是这会存在一个问题,https的默认端口是443,http的默认端口是80。也就是说端口设置为443和80时,直接访问域名即可进入主页,例:http://域名/ ,https://域名/。但是设置为8443后需要在域名后加端口号才可以访问应用主页,例:https://域名:8443/,http://域名:8080/ 。很不舒服,针对这个问题可以使用端口映射的方式解决,使用root用户执行以下命令:

    #443端口映射到8443
    iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443

     现在·https://域名/ ·即可直接使用8443端口了。或者可以使用Nginx等做反向代理也可以解决这个问题。