서버 운영/APM 서버 구축

호스트에서 버추얼 박스 가상머신 내 서버에 SSH/SFTP 접속

씨실과 날실 2021. 4. 5. 09:00

 

0. 작업 조건

작업 조건

가상머신 : VirtualBox 6.1.18

Host OS : bWindows 10 20H2

Guest OS : Ubuntu 20.04.2 LTS

 

 

 

1. 가상머신 포트포워딩 설정

버추얼박스 가상머신의 설정에서 네트워크 메뉴의 어댑터 탭에서 사용할 네트워크 어댑터는 기본적으로 NAT으로 설정되어 있습니다.

고급을 누르면 숨겨진 메뉴가 나타나는데 하단에 포트포워딩 버튼이 있습니다.

포트포워딩 버튼을 누르면 포트포워딩 규칙을 설정하는 창이 나타납니다.

 

포트포워딩 규칙의 이름과 호스트와 게스트의 포트 번호를 지정해줍니다. 호스트와 게스트의 IP는 따로 지정해줄 필요 없습니다. 버추얼박스가 자동으로 감지하여 적용해줍니다.

참고로 SSH와 SFTP의 기본 포트는 일반적으로 22번 포트를 사용합니다.

 

 

2. 호스트 VirtualBox Host-Only Network IP 구성 확인

호스트의 윈도 명령 프롬프트창(CMD)창을 실행하여 ipconfig 명령을 실행해줍니다.

Microsoft Windows [Version 10.0.19042.867]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\admin>ipconfig

Windows IP 구성


이더넷 어댑터 VirtualBox Host-Only Network:

   연결별 DNS 접미사. . . . :
   링크-로컬 IPv6 주소 . . . . : 생략
   IPv4 주소 . . . . . . . . . : 192.168.56.1
   서브넷 마스크 . . . . . . . : 255.255.255.0
   기본 게이트웨이 . . . . . . :


- 중략 -


이더넷 어댑터 이더넷:

   연결별 DNS 접미사. . . . :
   링크-로컬 IPv6 주소 . . . . : xxxx::xxxx:xxxx:xxxx:xxxx%x
   IPv4 주소 . . . . . . . . . : 192.168.xxx.xxx
   서브넷 마스크 . . . . . . . : 255.255.255.0
   기본 게이트웨이 . . . . . . : 192.168.200.254

C:\Users\admin>

위와 같이 호스트 컴퓨터의 IP와 버추얼박스 호스트 전용 네트워크의 IP를 확인할 수 있습니다.

 

 

3. 가상머신 내 OS 패키지 업그레이드

study@study-VirtualBox:~$ sudo apt update

study@study-VirtualBox:~$ sudo apt upgrade

가상머신을 구동하여 시스템 내 설치되어 있는 모든 패키지들을 업그레이드해줍니다.

위 명령은 데비안/우분투 계열 OS에서 패키지들을 업그레이드하는 명령입니다.

 

4. 가상머신 네트워크 확인

study@study-VirtualBox:~$ ip addr show

가상머신 내 게스트 시스템의 IP 주소 구성을 확인해줍니다.

 

study@study-VirtualBox:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:bd brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
       valid_lft 86319sec preferred_lft 86319sec
    inet6 xxxx::xxxx:xxxx:xxxx:xxxx/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
study@study-VirtualBox:~$ 

루프백((Loopback) 아이피인 127.0.0.1과 게스트 시스템(enp03)에 할당된 아이피를 확인할 수 있습니다.

 

5. 네트워크 도구 설치

study@study-VirtualBox:~$ ifconfig

이 명령을 통해서도 아래와 같이 리눅스 시스템의 네트워크 구성을 손쉽게 알 수 있습니다.

 

study@study-VirtualBox:~$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 xxxx::xxxx:xxx:xxxx:xxxx  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:3a:59:bd  txqueuelen 1000  (Ethernet)
        RX packets 200  bytes 215048 (215.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 17333 (17.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 169  bytes 14779 (14.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 169  bytes 14779 (14.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

study@study-VirtualBox:~$ 

 

그러나 이 명령은 항상 사용할 수 있는 것은 아닙니다. 이 명령을 사용하기 위해선 이 명령을 지원하는 네트워크 도구 패키지가 설치되어 있어야 합니다.

study@study-VirtualBox:~$ ifconfig

명령어 'ifconfig' 을(를) 찾을 수 없습니다. 그러나 다음을 통해 설치할 수 있습니다:

sudo apt install net-tools

study@study-VirtualBox:~$ 

만약 설치되어 있지 않다면 위와 같은 안내 메시지가 출력됩니다.

해당 명령을 사용하기 위해서는 net-tools 패키지를 설치해주어야 한다고 친절히 안내해주고 있군요.

 

study@study-VirtualBox:~$ sudo apt install net-tools
[sudo] study의 암호: 
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
다음 새 패키지를 설치할 것입니다:
  net-tools
0개 업그레이드, 1개 새로 설치, 0개 제거 및 0개 업그레이드 안 함.
196 k바이트 아카이브를 받아야 합니다.
이 작업 후 864 k바이트의 디스크 공간을 더 사용하게 됩니다.
받기:1 http://mirror.kakao.com/ubuntu focal/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
내려받기 196 k바이트, 소요시간 0초 (2,350 k바이트/초)
Selecting previously unselected package net-tools.
(데이터베이스 읽는중 ...현재 210435개의 파일과 디렉터리가 설치되어 있습니다.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb ..
.
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
net-tools (1.60+git20180626.aebd88e-1ubuntu1) 설정하는 중입니다 ...
Processing triggers for man-db (2.9.1-1) ...
study@study-VirtualBox:~$ 

해당 패키지를 설치해줍니다.

 

사실 이 글의 주제에만 초점을 맞춘다면 이번 단계 작업은 굳이 필요 없습니다. 그러나 나중에라도 각종 네트워크 관련 작업을 하기 위해서 해당 패키지를 설치하시는 것을 권장합니다.

 

6. OpenSSH 서버 설치

SSH나 SFTP 서비스를 사용하기 위해서는 OpenSSH 서버를 설치해주어야 합니다.

study@study-VirtualBox:~$ sudo apt install openssh-server
[sudo] study의 암호: 
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
다음의 추가 패키지가 설치될 것입니다 :
  ncurses-term openssh-sftp-server ssh-import-id
제안하는 패키지:
  molly-guard monkeysphere ssh-askpass
다음 새 패키지를 설치할 것입니다:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
0개 업그레이드, 4개 새로 설치, 0개 제거 및 0개 업그레이드 안 함.
688 k바이트 아카이브를 받아야 합니다.
이 작업 후 6,010 k바이트의 디스크 공간을 더 사용하게 됩니다.
계속 하시겠습니까? [Y/n] y
받기:1 http://mirror.kakao.com/ubuntu focal/main amd64 ncurses-term all 6.2-0ubuntu2 [249 kB]
받기:2 http://mirror.kakao.com/ubuntu focal-updates/main amd64 openssh-sftp-server amd64 1:8.2p1-4ubuntu0.2 [51.5 kB]
받기:3 http://mirror.kakao.com/ubuntu focal-updates/main amd64 openssh-server amd64 1:8.2p1-4ubuntu0.2 [377 kB]
받기:4 http://mirror.kakao.com/ubuntu focal/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB]
내려받기 688 k바이트, 소요시간 0초 (1,883 k바이트/초)
패키지를 미리 설정하는 중입니다...
Selecting previously unselected package ncurses-term.
(데이터베이스 읽는중 ...현재 210435개의 파일과 디렉터리가 설치되어 있습니다.)
Preparing to unpack .../ncurses-term_6.2-0ubuntu2_all.deb ...
Unpacking ncurses-term (6.2-0ubuntu2) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a8.2p1-4ubuntu0.2_amd64.deb ...
Unpacking openssh-sftp-server (1:8.2p1-4ubuntu0.2) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a8.2p1-4ubuntu0.2_amd64.deb ...
Unpacking openssh-server (1:8.2p1-4ubuntu0.2) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_5.10-0ubuntu1_all.deb ...
Unpacking ssh-import-id (5.10-0ubuntu1) ...
openssh-sftp-server (1:8.2p1-4ubuntu0.2) 설정하는 중입니다 ...
openssh-server (1:8.2p1-4ubuntu0.2) 설정하는 중입니다 ...

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:lT7qA25mKqcaVI2vERIM5jEhf9t32Bamqe70cnhtlO0 root@study-VirtualBox (R
SA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:kO4Jbgk/vH4pfT8bmOouTG758vnkm5V94dHDCm4e4pE root@study-VirtualBox (EC
DSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:qMLOAs8mmPjpFPO4SsYAL717yivP43rYRW3+mn20uFE root@study-VirtualBox (ED
25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.servi
ce.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/s
ystemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
ssh-import-id (5.10-0ubuntu1) 설정하는 중입니다 ...
Attempting to convert /etc/ssh/ssh_import_id
ncurses-term (6.2-0ubuntu2) 설정하는 중입니다 ...
Processing triggers for systemd (245.4-4ubuntu3.5) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ufw (0.36-6) ...
프로필 'Apache Full'용 규칙이 업데이트되었습니다
방화벽을 다시 읽었습니다
study@study-VirtualBox:~$ 

위와 같이 OpenSSH 서버를 설치해줍니다.

 

OpenSSH 서버를 정상적으로 설치하면 기본적으로 자동으로 구동됩니다.

study@study-VirtualBox:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
     Active: active (running) since Mon 2021-03-29 12:39:13 KST; 22s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 3038 (sshd)
      Tasks: 1 (limit: 4638)
     Memory: 1.1M
     CGroup: /system.slice/ssh.service
             └─3038 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

 3월 29 12:39:13 study-VirtualBox systemd[1]: Starting OpenBSD Secure Shell ser>
 3월 29 12:39:13 study-VirtualBox sshd[3038]: Server listening on 0.0.0.0 port >
 3월 29 12:39:13 study-VirtualBox sshd[3038]: Server listening on :: port 22.
 3월 29 12:39:13 study-VirtualBox systemd[1]: Started OpenBSD Secure Shell serv>
lines 1-15/15 (END)

 혹시 모르니 OpenSSH 서버가 제대로 구동되고 있는지 확인해봅니다.

 

7. 방화벽 설정

OpenSSH 서버를 이용해 SSH/SFTP를 사용하기 위해서는 해당 기능을 사용할 포트를 열어줍니다.

 

먼저 시스템의 포트 상태를 확인해줍니다.

study@study-VirtualBox:~$ sudo ufw status
상태: 활성

목적                         동작          출발
--                         --          --
Apache Full                ALLOW       Anywhere                  
Apache Full (v6)           ALLOW       Anywhere (v6)             

study@study-VirtualBox:~$ sudo ufw status verbose
상태: 활성
로깅: on (low)
기본 설정: deny (내부로 들어옴), allow (외부로 나감), disabled (라우팅 된)
새 프로필: skip

목적                         동작          출발
--                         --          --
80,443/tcp (Apache Full)   ALLOW IN    Anywhere                  
80,443/tcp (Apache Full (v6)) ALLOW IN    Anywhere (v6)             

study@study-VirtualBox:~$ 

현재 방화벽이 활성화되어 있고, 웹서버가 사용하는 HTTP, HTTPS 프로토콜용 80, 443 포트만 열려 있는 것을 확인할 수 있습니다.

 

study@study-VirtualBox:~$ sudo ufw allow ssh
규칙이 추가되었습니다
규칙이 추가되었습니다 (v6)
study@study-VirtualBox:~$ sudo ufw reload
방화벽을 다시 읽었습니다
study@study-VirtualBox:~$ 

이제 방화벽 프로그램의 포트 개방 명령을 실행해줍니다.

우분투의 기본 방화벽 프로그램인 ufw의 포트 개방 명령은 다음과 같습니다.

study@study-VirtualBox:~$ sudo ufw allow 포트 번호

그러나 위에서는 포트 번호 대신 ssh를 적었는데요. 있는 ufw의 경우 주요 서버 서비스들이 사용하는 기본 포트는 굳이 숫자가 아니라 해당 서비스명을 대신 입력해도 됩니다.

 

포트 개방 명령을 실행하면 일반적으로 방화벽 설정을 자동으로 다시 읽어들여 적용하지만 어떤 이유로 그러지 않을 수도 있습니다. 따라서 reload 명령을 실행하여 추가 또는 수정한 방화벽 설정을 다시금 읽어들입니다.

 

study@study-VirtualBox:~$ sudo ufw status
상태: 활성

목적                         동작          출발
--                         --          --
Apache Full                ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
Apache Full (v6)           ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)             

study@study-VirtualBox:~$ sudo ufw status verbose
상태: 활성
로깅: on (low)
기본 설정: deny (내부로 들어옴), allow (외부로 나감), disabled (라우팅 된)
새 프로필: skip

목적                         동작          출발
--                         --          --
80,443/tcp (Apache Full)   ALLOW IN    Anywhere                  
22/tcp                     ALLOW IN    Anywhere                  
80,443/tcp (Apache Full (v6)) ALLOW IN    Anywhere (v6)             
22/tcp (v6)                ALLOW IN    Anywhere (v6)             

study@study-VirtualBox:~$ 

다시 방화벽 상태를 확인합니다.

ssh의 기본 포트인 22번 포트가 추가로 열린 것을 확인할 수 있습니다.

 

8. putty로 접속

 

이제 호스트 PC에서 putty를 실행하여 가상머신의 게스트에 접속해봅시다.

 

login as: study
study@127.0.0.1's password:
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-48-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0개의 업데이트가 바로 설치 가능합니다.
0개의 업데이트는 보안 업데이트입니다.

Your Hardware Enablement Stack (HWE) is supported until April 2025.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

study@study-VirtualBox:~$ 

 

 

9. SFTP 접속

 

 호스트 PC에서 Filezilla를 실행하여 가상머신의 게스트에 접속해봅시다.