자바스크립트를 활성화 해주세요

p056 Windows에서 리눅스로 Pwsh Remote접속하기

 ·  ☕ 3 min read

Linux to Linux 는 서로 ssh 가 설치되어 있기 때문에 따로 설치하지 않고, sshd_config의 설정 추가만으로 remote powershell을 할 수 있다고 설명했습니다.

이번에는, Windows to Linux를 시도합니다. 역시 ssh를 설치해야하는 과정이 추가됩니다.

win32-openssh 설치

  • Windows 10이나 Server 2019 라면, 제어판 기능추가에서 설치하는 방법이 있습니다.
    • App > Apps > Settings & Apps > Manage Optional Features > Add Feature
  • 만일 powershell 사용이 가능하시다면, 다음의 방법도 있습니다.
    1
    2
    3
    4
    5
    6
    7
    
    Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
      
    # Install the OpenSSH Client
    Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
      
    # Install the OpenSSH Server
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
    

하지만 안타깝게도 저는 Server 2016을 사용하고 있습니다. 이런 경우에는

혹시 Scoop을 알고 계신다면 Scoop 으로 설치하셔도 됩니다.

scoop install Win32-OpenSSH

p056_scoop_install_win32-openssh.png

몇가지 중요한 메시지가 나옵니다.

  • 저는 Gow를 사용하고 있었는데 Gow에서 제공하는 Ssh의 링크가 덮어써지게 됩니다.
  • sshd 서비스를 설치하려면 어떤 스크립트를 실행하라고 합니다.
Run 'sudo C:\Users\Administrator\scoop\apps\win32-openssh\current\install-sshd.ps1' to install sshd and ssh-agent as a service.
Run 'sudo C:\Users\Administrator\scoop\apps\win32-openssh\current\uninstall-sshd.ps1' to uninstall the services.

서비스를 설치하는 스크립트와 삭제하는 스크립트가 있으니 실행하라는 설명이 나옵니다. 실은 그 외에도 몇몇 파일들은 권한설정이 접근하기 어렵게 설정할 필요도 있습니다. Win32-OpenSSH가 설치된 폴더를 살펴보면 몇몇 script가 더 있습니다

p056_win32-openssh_files.png

실행

C:\ProgramData\ssh 라는 폴더는 서비스를 실행하기 전에는 존재하지 않는데, start-service sshd를 하고 난 뒤에는 폴더와 여러가지 파일들이 생성됩니다.

생성된 파일들의 권한이 제대로 되어 있지 않은 경우를 위해서, 이를 수정해 주는 스크립트가 존재합니다.

apps/win32-openssh/current
❯ get-service sshd

Status   Name               DisplayName
------   ----               -----------
Stopped  sshd               OpenSSH SSH Server


apps/win32-openssh/current
❯ start-service sshd

apps/win32-openssh/current
❯ dir C:\ProgramData\ssh


    Directory: C:\ProgramData\ssh

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2020/09/24    19:03                logs
-a---          2020/09/24    19:03           1401 ssh_host_dsa_key
-a---          2020/09/24    19:03            619 ssh_host_dsa_key.pub
-a---          2020/09/24    19:03            525 ssh_host_ecdsa_key
-a---          2020/09/24    19:03            191 ssh_host_ecdsa_key.pub
-a---          2020/09/24    19:03            419 ssh_host_ed25519_key
-a---          2020/09/24    19:03            111 ssh_host_ed25519_key.pub
-a---          2020/09/24    19:03           2622 ssh_host_rsa_key
-a---          2020/09/24    19:03            583 ssh_host_rsa_key.pub
-a---          2020/09/24    19:03              7 sshd.pid
-a---          2019/12/18     7:48           2297 sshd_config


apps/win32-openssh/current
❯ . ./FixHostFilePermissions.ps1
  [*] C:\ProgramData\ssh\sshd_config

Need to remove the inheritance before repair the rules.
Shall I remove the inheritace?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
Inheritance is removed from 'C:\ProgramData\ssh\sshd_config'.

'BUILTIN\Users' should not have access to 'C:\ProgramData\ssh\sshd_config'..
Shall I remove this access?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
'BUILTIN\Users' has no more access to 'C:\ProgramData\ssh\sshd_config'.
      Repaired permissions

  [*] C:\ProgramData\ssh\ssh_host_dsa_key
      looks good

  [*] C:\ProgramData\ssh\ssh_host_dsa_key.pub
      looks good

  [*] C:\ProgramData\ssh\ssh_host_ecdsa_key
      looks good

  [*] C:\ProgramData\ssh\ssh_host_ecdsa_key.pub
      looks good

  [*] C:\ProgramData\ssh\ssh_host_ed25519_key
      looks good

  [*] C:\ProgramData\ssh\ssh_host_ed25519_key.pub
      looks good

  [*] C:\ProgramData\ssh\ssh_host_rsa_key
      looks good

  [*] C:\ProgramData\ssh\ssh_host_rsa_key.pub
      looks good

   Done.


apps/win32-openssh/current took 6s
❯

필요하다면 서비스가 자동으로 실행되도록 설정합니다.

1
Set-Service -Name sshd -StartupType 'Automatic'

파이어월 설정을 확인합니다. Inbound가 가능한 설정이 있으면 됩니다.

1
Get-NetFirewallRule -Name *ssh*

없으면 하나 만듭니다.

1
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

사실 윈도우즈가 클라이언트고, 리눅스가 서버이기 때문에 만들지 않아도 됩니다.

파이어월 설정문서는 다음을 참고합니다.

pssession

1
2
$session = New-PSSession -HostName $host -UserName $username
enter-pssession $session

p056_pssession_from_windows_to_linux.png

이를 이용하면 파일을 copy할 수도 있고,

Copy-Item -Path "C:\temp\jdk-8u172-linux-x64.tar.gz" -ToSession $session -Destination "/root"

여러가지가 할 수 있습니다만, 몇 가지 알아두어야 할 점이 있습니다.

  • ssh로 세션을 열 수 있는 것은 Pwsh Core입니다. 윈도우즈용 powershell은 WinRM만 사용합니다.
  • 그렇기 때문에 vscode의 powershell integrated Console을 사용해서 psedit 할 수 없습니다.
  • 또 remote session에 연결되었을 때, 다른 쉘을 실행할 수 없습니다. 터미널이 아니라는 메시지가 나올 수 있습니다.
  • vim을 실행할 수 없읍니다. 다음과 같은 메시지가 나올 수 있습니다.
    Vim: Warning: Output is not to a terminal
    Vim: Warning: Input is not from a terminal
    
    Error: The pipeline has been stopped.
    

아마도 다음의 이미지가 이유를 설명해 줄 것 같습니다.
PowerShell_Remoting_SSH

이 이미지가 있는 문서는 OpenSSH대신 socat을 사용하는 것도 가능하다고 합니다만, 덜 실용적인 것 같습니다.

https://blog.devolutions.net/2020/07/powershell-remoting-over-ssh-without-ssh

레퍼런스

공유하기

tkim
글쓴이
tkim
Software Engineer