[原创] Unhandled exception. System.Net.Sockets.SocketException (13): Permission denied 原因
将项目部署到ubuntu上面以后,运行,竟然报错:
ubuntu@VM-4-11-ubuntu:~/publish$ uname -a
Linux VM-4-11-ubuntu 5.4.0-121-generic #137-Ubuntu SMP Wed Jun 15 13:33:07 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@VM-4-11-ubuntu:~/publish$
ubuntu@VM-4-11-ubuntu:~/publish$ ./qhxyhs
==================西宁威势电子提供此程序==================8/27/2022 7:07:25 PM: 数据库初始化专题数量1条
8/27/2022 7:07:26 PM: 数据库初始化频道数量5条
8/27/2022 7:07:26 PM: 数据库初始化系统权限模块数量47条
8/27/2022 7:07:26 PM: 数据库初始化系统用户数量3条
8/27/2022 7:07:26 PM: 数据库初始化用户角色数量3条
8/27/2022 7:07:26 PM: 数据库初始化API商家演示接口数量5条
8/27/2022 7:07:26 PM: 数据库初始化频道单页面数量3条
8/27/2022 7:07:26 PM: 数据库初始化频道新闻页面5条
8/27/2022 7:07:26 PM: 数据库初始化完成.
Unhandled exception. System.Net.Sockets.SocketException (13): Permission denied
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError , String )
at System.Net.Sockets.Socket.DoBind(EndPoint , SocketAddress )
at System.Net.Sockets.Socket.Bind(EndPoint )
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken )
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost , CancellationToken )
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost , CancellationToken )
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost )
at WinsMVC.Program.Main(String[] args) in X:\WEB\Program.cs:line 41
Aborted (core dumped)
看提示,应该是权限拒绝引起的。在lunchSetting.json中有如下定义:
"applicationUrl": "https://www.xxx.com;http://www.xxx.com;",
奇怪的是,将https协议剔除以后再次部署运行,程序又可以正常启动,但是端口被绑定到5000上面去了。于是在startup.cs中强行绑定端口试试:
webBuilder.UseKestrel() .UseUrls($"http://"+System.Net.IPAddress.Any+":80") .UseStartup<Startup>();
但是还是报同样的错。后来经过一凡查阅资料与测试,发现绑定低端端口的时候就会报错,将端口改为3389后再运行,发现代码又可以正常运行了,高端口( >1024)没有问题。看来确实是权限原因引起的,如果是root权限,应该是没问题的,条件不成熟,没有测试。
这些是查阅到的资料
PROBLEM: Rancher reported "unable to start kestrel" and Access Denied
REASON: This happen because port 80 is low port and need adminstrator permissions
SOLUTION: Use port 8080