Linux系统不能以非root用户绑定80端口

Blade 未结 1 85
whale
whale 2024-12-10 13:15

一、该问题的重现步骤是什么?

1. debug启动 'Application'


二、你期待的结果是什么?实际看到的又是什么?

期待:是看到运行成功

实际:出现报错Caused by: java.net.BindException: Permission denied

at java.base/sun.nio.ch.Net.bind0(Native Method)

at java.base/sun.nio.ch.Net.bind(Net.java:555)

at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:337)

at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:294)

at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:89)

at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:178)

at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:310)

at io.undertow.Undertow.start(Undertow.java:196)

... 16 common frames omitted


Disconnected from the target VM, address: '127.0.0.1:33027', transport: 'socket'


三、你正在使用的是什么产品,什么版本?在什么操作系统上?


SpringBoot版本,Ubuntu20

四、请提供详细的错误堆栈信息,这很重要。


Connected to the target VM, address: '127.0.0.1:33027', transport: 'socket'

Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts

----启动中,读取到的环境变量:[dev],jar地址:[/home/zjm/BladeX/boot/target/classes/]----

2024-12-10 13:04:11.566  INFO 32935 --- [kground-preinit] o.h.validator.internal.util.Version      : HV000001: Hibernate Validator 8.0.1.Final

 _____               _                 ______  _             _

/  ___|             (_)                | ___ \| |           | |

\ `--.  _ __   _ __  _  _ __    __ _   | |_/ /| |  __ _   __| |  ___

 `--. \| '_ \ | '__|| || '_ \  / _` |  | ___ \| | / _` | / _` | / _ \

/\__/ /| |_) || |   | || | | || (_| |  | |_/ /| || (_| || (_| ||  __/

\____/ | .__/ |_|   |_||_| |_| \__, |  \____/ |_| \__,_| \__,_| \___|

       | |                      __/ |

       |_|                     |___/


:: SpringBlade :: blade-api:dev :: Running SpringBoot 3.2.10 :: 


2024-12-10 13:04:11.609  INFO 32935 --- [           main] org.springblade.Application              : Starting Application using Java 17.0.13 with PID 32935 (/home/zjm/BladeX/boot/target/classes started by zjm in /home/zjm/BladeX/boot)

2024-12-10 13:04:11.609 DEBUG 32935 --- [           main] org.springblade.Application              : Running with Spring Boot v3.2.10, Spring v6.1.14

2024-12-10 13:04:11.610  INFO 32935 --- [           main] org.springblade.Application              : The following 1 profile is active: "dev"

2024-12-10 13:04:12.481  INFO 32935 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode

2024-12-10 13:04:12.483  INFO 32935 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.

2024-12-10 13:04:12.522  INFO 32935 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 31 ms. Found 0 Redis repository interfaces.

2024-12-10 13:04:12.583  WARN 32935 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[org.springblade]' package. Please check your configuration.

2024-12-10 13:04:12.670  INFO 32935 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor init.

2024-12-10 13:04:12.705  INFO 32935 --- [           main] s.c.l.p.BladePropertySourcePostProcessor : BladePropertySourcePostProcessor process @BladePropertySource bean.

2024-12-10 13:04:13.214  WARN 32935 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used

2024-12-10 13:04:13.226  INFO 32935 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext

2024-12-10 13:04:13.227  INFO 32935 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1585 ms

2024-12-10 13:04:13.726  INFO 32935 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource

2024-12-10 13:04:13.789  INFO 32935 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited

2024-12-10 13:04:16.205  INFO 32935 --- [           main] o.s.core.boot.config.RetryConfiguration  : configServerRetryInterceptor: Changing backOffOptions to initial: 1000, multiplier: 1.2, maxInterval: 5000

2024-12-10 13:04:16.221  INFO 32935 --- [           main] o.s.core.log.logger.BladeLogger          : blade-api: BladeLogger init success!

2024-12-10 13:04:16.936  INFO 32935 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'

2024-12-10 13:04:17.022  INFO 32935 --- [           main] io.undertow                              : starting server: Undertow - 2.3.17.Final

2024-12-10 13:04:17.027  INFO 32935 --- [           main] org.xnio                                 : XNIO version 3.8.16.Final

2024-12-10 13:04:17.032  INFO 32935 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.16.Final

2024-12-10 13:04:17.047  INFO 32935 --- [           main] org.jboss.threads                        : JBoss Threads version 3.5.0.Final

2024-12-10 13:04:17.070  INFO 32935 --- [           main] io.undertow                              : stopping server: Undertow - 2.3.17.Final

2024-12-10 13:04:17.076  WARN 32935 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'

2024-12-10 13:04:17.083  INFO 32935 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closing ...

2024-12-10 13:04:17.083  INFO 32935 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed

2024-12-10 13:04:17.099  INFO 32935 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 


Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.

2024-12-10 13:04:17.109 ERROR 32935 --- [           main] o.s.boot.SpringApplication               : Application run failed


org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:288)

at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:472)

at java.base/java.lang.Iterable.forEach(Iterable.java:75)

at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:257)

at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:202)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:990)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:628)

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)

at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:149)

at org.springblade.core.launch.BladeApplication.run(BladeApplication.java:49)

at org.springblade.Application.main(Application.java:33)

Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Undertow

at org.springframework.boot.web.embedded.undertow.UndertowWebServer.start(UndertowWebServer.java:132)

at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:44)

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:285)

... 13 common frames omitted

Caused by: java.lang.RuntimeException: java.net.BindException: Permission denied

at io.undertow.Undertow.start(Undertow.java:254)

at org.springframework.boot.web.embedded.undertow.UndertowWebServer.start(UndertowWebServer.java:118)

... 15 common frames omitted

Caused by: java.net.BindException: Permission denied

at java.base/sun.nio.ch.Net.bind0(Native Method)

at java.base/sun.nio.ch.Net.bind(Net.java:555)

at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:337)

at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:294)

at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:89)

at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:178)

at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:310)

at io.undertow.Undertow.start(Undertow.java:196)

... 16 common frames omitted


Disconnected from the target VM, address: '127.0.0.1:33027', transport: 'socket'


Process finished with exit code 1


五、若有更多详细信息,请在下面提供。


报错原因应该是Linux不支持普通用户使用80端口。(低于 1024 的端口)

有哪些优雅的方式解决这个问题。我修改了application.yml中server.port为8088后可以正常运行,但是访问http://localhost/doc.html 显示

This site can’t be reached

localhost refused to connect.


1条回答
  •  admin
    admin (楼主)
    2024-12-10 13:23

    普通用户无法直接使用低于 1024 的端口(如 80 端口)是由于 Linux 的安全限制。可以通过以下几种方法解决:

    1.使用 setcap:授予 Java 进程绑定低端口的权限。

    2.使用 iptables 转发端口:将 80 端口流量转发到高端口。

    3.使用 authbind:允许普通用户绑定到低端口。

    4.使用 systemd:通过配置 systemd 服务并授予特权来解决问题。


    具体操作可对应查询一下相关博文。但如果不想麻烦,直接改成1024以上端口启动便可


    0 讨论(0)
提交回复