본문 바로가기

CPU

Docker Container ssh 설정

Port Forwarding

container에서 기본적으로 ssh에 사용할 port를 forwarding 해줘야 함

 

작업 스케줄러 설정

*.ps1 파일 생성

윈도우 작업 스케줄러 설정(컴퓨터 시작 시 실행됨)

wsl-connect-external.ps1

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);

#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";

#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}

 

Excution Policy 설정

policy 오류 발생시 아래 커맨드 실행

PowerShell.exe -ExecutionPolicy Bypass -File .\\wsl-forward-server.ps1

 

Container

volume 설정, port forwarding

docker run --gpus all -it --ipc host -p 22:22 -p 8888:8888 -p 5000:5000 -p 6379:6379 -p 50:50 -v [volume] node --name [container] [image]

 

Root pw 변경

passwd root

 

Config file 수정

vi /etc/ssh/sshd_config

Port 22
PasswordAuthentication yes

 

방화벽

SSH 포트가 방화벽으로 막혀있을 경우 접속이 불가능하다.

  1. '제어판 > 시스템 및 보안 > Windows Defender 방화벽’에 들어가 '고급 설정’을 클릭한다.
  2. 인바운드 규칙을 클릭하고 '새 규칙’을 클릭한다.
  3. 규칙 종류는 '포트’를 선택한다.
  4. 프로토콜은 TCP, '특정 원격 포트’는 앞서 설정한 Port 값으로 설정한다. (2222) 이름은 마음대로 정한다.
  5. 아웃바운드 규칙도 마찬가지로 해준다.

 

ssh

sudo service ssh start

 

Reference

https://m.blog.naver.com/seongjin0526/221778212779

https://tttsss77.tistory.com/155

https://codeac.tistory.com/118

https://hnsuk.tistory.com/49