Answered by:
Permission denied when writing to a folder after deploying to linux server

Question
-
User-1820491116 posted
Hi,
i have been getting Permission denied IOexception after deploying a .net core 5 project to a linux ubuntu 20.04, this happens when i try to write to any folder inside my application like for an example changing profile picture .
i've tried the following command to change the permissions of the folder "ProfilePictures" but it is not working
chmod -R 0755 /var/www/elseforty.com/publish/wwwroot/Uploads/ProfilePictures/
here is the full exception message
IOException: Permission denied Show raw exception details UnauthorizedAccessException: Access to the path '/var/www/elseforty.com/publish/wwwroot/Uploads/ProfilePictures/_de55.jpg' is denied. Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, string path, bool isDirectory, Func<ErrorInfo, ErrorInfo> errorRewriter) Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string path, OpenFlags flags, int mode) System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options) System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) System.IO.File.Create(string path) ElseForty.FileServices.Files+Server.UploadFileAsync(IFormFile file, string fullFolderPath) in Files.cs ElseForty.Controllers.AccountController.EditProfile(User_EditProfileViewModel model) in AccountController.cs Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments) System.Threading.Tasks.ValueTask<TResult>.get_Result() System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult() Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
any help is appreciated
Saturday, May 8, 2021 8:23 PM
Answers
-
User-1820491116 posted
i've managed to find a solution for this,
you don't need to break your head with code that will change folders permissions , all you have to do is to set the parent folder permissions manually with recursive being set to true, this will automatically grant the parent folder permissions to the children folders
for an example i have a structure like this /MyProject/wwwroot/Uploads/{my dynamically created folders here}
all i did is set Read, Write, Execute permissions on the folder {Uploads} using the command bellow
chmod -R 757 /var/www/elseforty.com/wwwroot/Uploads
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 9, 2021 12:15 PM
All replies
-
User-474980206 posted
Only the folder owner has write access to files or folder. Use ls -l to get the owners name of the files and folder. Then use ps -u to what user your asp.net process is running under.
Saturday, May 8, 2021 10:09 PM -
User-1820491116 posted
Hi bruce,
the folders are owner by the 'root' user, the ps -u is returning this, i don't know what can i do with this
root@localhost:~# ps -u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1342 0.0 0.0 7352 1932 ttyS0 Ss+ May06 0:00 /sbin/agetty root 1344 0.0 0.0 5828 1524 tty1 Ss+ May06 0:00 /sbin/agetty root 138589 0.0 0.2 8308 4960 pts/0 Ss 01:29 0:00 -bash root 142620 0.0 0.1 8892 3196 pts/0 R+ 02:12 0:00 ps -u
i've managed to set the folder permission manually from filezila, it works as expected
folder=>(right click)File Attributes
but i want to do it automatically since i'm creating and deleting folders on my server from my admin pannel
i tried also this solution but it is not working as well
public static bool Exec(string filePath, string permissions = "700", bool recursive = false) { string cmd; if (recursive) cmd = $"chmod -R {permissions} {filePath}"; else cmd = $"chmod {permissions} {filePath}"; try { using (Process proc = Process.Start("/bin/bash", $"-c \"{cmd}\"")) { proc.WaitForExit(); return proc.ExitCode == 0; } } catch { return false; } }
Files.Server.Exec("/var/www/elseforty.com/wwwroot/Uploads/ProfilePictures", "757", true);
any help is welcome
Sunday, May 9, 2021 2:23 AM -
User-1820491116 posted
i've managed to find a solution for this,
you don't need to break your head with code that will change folders permissions , all you have to do is to set the parent folder permissions manually with recursive being set to true, this will automatically grant the parent folder permissions to the children folders
for an example i have a structure like this /MyProject/wwwroot/Uploads/{my dynamically created folders here}
all i did is set Read, Write, Execute permissions on the folder {Uploads} using the command bellow
chmod -R 757 /var/www/elseforty.com/wwwroot/Uploads
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 9, 2021 12:15 PM