Skip to content

Instantly share code, notes, and snippets.

@QZLin
Created February 2, 2025 15:42
Show Gist options
  • Select an option

  • Save QZLin/ba2ac4519fd585804c96003f2de2eacf to your computer and use it in GitHub Desktop.

Select an option

Save QZLin/ba2ac4519fd585804c96003f2de2eacf to your computer and use it in GitHub Desktop.
Fix authorized_keys Permissions for windows
# Define the file path
$filePath = "$env:USERPROFILE\.ssh\authorized_keys"
# Check if the file exists
if (-Not (Test-Path $filePath)) {
Write-Host "File $filePath does not exist."
exit
}
# Get the current file's ACL (Access Control List)
$acl = Get-Acl -Path $filePath
# Disable inheritance
$acl.SetAccessRuleProtection($true, $false)
# Get the current user
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# Define required permissions
$requiredPermissions = @(
[System.Security.AccessControl.FileSystemRights]::ReadAndExecute,
[System.Security.AccessControl.FileSystemRights]::Read,
[System.Security.AccessControl.FileSystemRights]::Write
)
# Add Full Control permission for the SYSTEM user
$systemIdentity = "NT AUTHORITY\SYSTEM"
$systemAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$systemIdentity,
[System.Security.AccessControl.FileSystemRights]::FullControl,
[System.Security.AccessControl.AccessControlType]::Allow
)
$acl.SetAccessRule($systemAccessRule)
# Add basic permissions for the current user
$userAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$currentUser,
[System.Security.AccessControl.FileSystemRights]::ReadAndExecute,
[System.Security.AccessControl.AccessControlType]::Allow
)
$acl.SetAccessRule($userAccessRule)
# Apply the modified ACL
Set-Acl -Path $filePath -AclObject $acl
Write-Host "Permissions for file $filePath have been successfully updated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment