딥러닝 서버 구축

hottogi·2023년 3월 16일
0
post-thumbnail

딥러닝 서버 구축기

이틀에 걸쳐 딥러닝 서버 구축 완료. 차후 또 활용하기 위해서 과정을 남깁니다.

개요

네이티브 리눅스로 구동하고자 하였으나 윈도우를 꼭 써야만 하는 상황이었습니다.

wsl2 라는 좋은 자원 발견. 안정적으로 윈도우와 리눅스를 동시에 구동할 수 있네요.

1. 파워셸 우분투 설치

파워셸에서 우분투를 설치하고 그 위에 openssh-server, cuda, cudnn등을 설치했습니다.

vi 편집기에서 22포트는 보안상 이슈가 있어서 원하는 포트로 변경, PasswordAutentication도 Yes로 변경하였습니다.

root 계정으로 편집하는게 권한 때문에 편리합니다.

sudo su - root

2. 우분투 방화벽 해제

우분투 방화벽을 해제하고

sudo ufw disable

3. 서버 오픈

서버 오픈을 해줍니다.

sudo service ssh stop
sudo systemctl start ssh
sudo systemctl enable ssh
sudo systemctl disable ssh
sudo /etc/init.d/ssh restart
sudo systemctl status ssh

systemctl 명령어 오류시

sudo -b unshare --pid --fork --mount-proc /lib/systemd/systemd --system-unit=basic.target
sudo -E nsenter --all -t $(pgrep -xo systemd) runuser -P -l $USER -c "exec $SHELL"

4. 파워셸 스크립트 작성

파워셸 스크립트를 작성합니다. 자세한건 아래 링크 참조해주세요.

$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, 1000,2000,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";
}

$ports=@(80,1000,2000,3000,5000); -> 개별적으로 vi편집기에서 오픈한 port 할당해 주세요.

5. vscode 서버 접속

구축된 서버를 vscode로 접속해봅니다.
ssh config 파일에 우분투 ip 대신 local ip를 할당해야 합니다. 포트포워딩을 해준 이유이기도 하지요.

6. cmd 접속

vscode 대신 cmd 접속 방법

ssh [name]@[ip]

default port:22 다른 port 사용 시 -p[port number] 추가해 주세요.

7. 마무리

이제 GPU를 원격으로 사용할 수 있게 됐습니다. 도커를 통해 아늑한 공간에서 작업하시면 됩니다.

8. 오류

ssh 접속이 불가능할 때 포괄적으로 뜨는 오류 메세지

프로세스에서 없는 파이프에 쓰려고 했습니다.

다양한 원인이 있지만 local에서 우분투로 ssh 접속이 가능하다면 local 내부의 문제는 아님. 적어도 우분투 방화벽 문제가 아님을 알 수 있음.
외부에서 local로 진입하지 못하는 상태이므로 wsl2 포트 포워딩, wsl2 방화벽 등에 문제가 있을 수 있고 외부 컴퓨터에서 ip입력을 잘못 했을 가능성도 있음. local의 우분투 ip가 아닌 local ip를 입력해야함. 또한 임의 설정한 포트를 config 파일에 지정해 주지 않았을 가능성도 존재.

9. 과정 요약 및 참조 링크

  1. 파워셸에서 우분투 설치(https://gaesae.com/161)
  2. openssh-server, cuda, cudnn 등 리눅스에 설치(https://medium.com/dawn-cau/wsl2-%EB%94%A5%EB%9F%AC%EB%8B%9D-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0-95d7b95d1f4b)
  3. wsl2와 vscode 연동
  4. vi 편집기에서 내가 원하는 번호로 포트 설정, PasswordAuthentication YES로 변경(https://myinbox.tistory.com/156)
  5. 우분투 방화벽 해제(https://webdir.tistory.com/206)
  6. 우분투 서버 오픈(https://www.simplified.guide/ssh/restart-service)
  7. local 환경에서 wsl2 연동한 vscode로 ssh 접속되는지 확인 (config파일에 우분투 아이피)
  8. 포트포워딩으로 wsl2의 우분투 ip와 내 컴퓨터의 ip를 연동시켜주기(4번에서 열어준 포트로 등록) (https://webisfree.com/2021-07-14/wsl2-%EC%99%B8%EB%B6%80-remote-ip-%EC%A0%91%EC%86%8D-%EA%B0%80%EB%8A%A5%ED%95%98%EB%8F%84%EB%A1%9D-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0-%EB%B0%A9%ED%99%94%EB%B2%BD-%ED%95%B4%EC%A0%9C)
  9. 외부 컴퓨터로 ssh 접속하기 (config파일에 우분투 아이피 대신 local 아이피 써야함.)
    cmd 접속시에는 ssh name@아이피 (default port : 22, 다른 port로 가고싶을 때는 -p[port number])
profile

0개의 댓글