admin管理员组

文章数量:1437831

takeown和icacls的配合使用

场景:Windows Cygwin环境,备份还原etc\ssh*.pub、etc\ssh*_key(合并的话是ssh_host_*key*)

备份:

代码语言:javascript代码运行次数:0运行复制
takeown /F "C:\cygwinroot\etc\ssh*.pub" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*.pub" /grant:r "Administrator:(F)" /T /C /Q
Xcopy /F /H /Y C:\cygwinroot\etc\ssh*.pub C:\keypub\

takeown /F "C:\cygwinroot\etc\ssh*_key" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*_key" /grant:r "Administrator:(F)" /T /C /Q
Xcopy /F /H /Y C:\cygwinroot\etc\ssh*_key C:\keypub\

还原:

代码语言:javascript代码运行次数:0运行复制
takeown /F "C:\cygwinroot\etc\ssh*.pub" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*.pub" /grant:r "Administrator:(F)" /T /C /Q

takeown /F "C:\cygwinroot\etc\ssh*_key" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*_key" /grant:r "Administrator:(F)" /T /C /Q

Xcopy /F /H /Y C:\keypub\ssh*.pub c:\cygwinroot\etc\
Xcopy /F /H /Y C:\keypub\ssh*_key c:\cygwinroot\etc\

如果cygwin sshd启动报错如下,

代码语言:javascript代码运行次数:0运行复制
PS C:\Users\Administrator>net start sshd
CYGWIN sshd 服务正在启动 .
CYGWIN sshd 服务无法启动。

服务没有报告任何错误。

请键入 NET HELPMSG 3534 以获得更多的帮助。

PS C:\Users\Administrator> NET HELPMSG 3534

服务没有报告任何错误。

则查看C:\cygwinroot\var\log\sshd.log如下,确认是权限问题导致

代码语言:javascript代码运行次数:0运行复制
Permissions 0070 for '/etc/ssh_host_rsa_key' are too open.
Permissions 0070 for '/etc/ssh_host_dsa_key' are too open.
Permissions 0070 for '/etc/ssh_host_ecdsa_key' are too open.
Permissions 0070 for '/etc/ssh_host_ed25519_key' are too open.

解决方案:提权Administrator后启动sshd服务

cmd、powershell均可

代码语言:javascript代码运行次数:0运行复制
takeown /F "C:\cygwinroot\etc\ssh*.pub" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*.pub" /grant:r "Administrator:(F)" /T /C /Q

takeown /F "C:\cygwinroot\etc\ssh*_key" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh*_key" /grant:r "Administrator:(F)" /T /C /Q

或者调整通配符合并为2句
takeown /F "C:\cygwinroot\etc\ssh_host_*key*" /A /R /D Y
icacls "C:\cygwinroot\etc\ssh_host_*key*" /grant:r "Administrator:(F)" /T /C /Q

仅powershell

代码语言:txt复制
cmd.exe /c 'takeown /F "C:\cygwinroot\etc\ssh*.pub" /A /R /D Y'
cmd.exe /c 'icacls "C:\cygwinroot\etc\ssh*.pub" /grant:r "Administrator:(F)" /T /C /Q'

cmd.exe /c 'takeown /F "C:\cygwinroot\etc\ssh*_key" /A /R /D Y'
cmd.exe /c 'icacls "C:\cygwinroot\etc\ssh*_key" /grant:r "Administrator:(F)" /T /C /Q'

或者调整通配符合并为2句
cmd.exe /c 'takeown /F "C:\cygwinroot\etc\ssh_host_*key*" /A /R /D Y'
cmd.exe /c 'icacls "C:\cygwinroot\etc\ssh_host_*key*" /grant:r "Administrator:(F)" /T /C /Q'

本文标签: takeown和icacls的配合使用