LXC instance 생성하기 및 기본 편의 설정

Onam Kwon·2024년 12월 19일
0

Ubuntu-LXD&LXC

목록 보기
1/3

컨테이너 목록 확인

lxc list
onam@Onam:/mnt/c/Users/kon64$ lxc list
+-------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| NAME  |  STATE  |             IPV4             |                     IPV6                     |   TYPE    | SNAPSHOTS |
+-------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| dev-1 | RUNNING | 172.18.0.1 (docker_gwbridge) | fd42:b762:7d5:b2f8:216:3eff:fe25:4931 (eth0) | CONTAINER | 0         |
|       |         | 172.17.0.1 (docker0)         |                                              |           |           |
|       |         | 10.64.213.81 (eth0)          |                                              |           |           |
+-------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| dev-2 | RUNNING | 172.18.0.1 (docker_gwbridge) | fd42:b762:7d5:b2f8:216:3eff:fe91:7df9 (eth0) | CONTAINER | 0         |
|       |         | 172.17.0.1 (docker0)         |                                              |           |           |
|       |         | 10.64.213.67 (eth0)          |                                              |           |           |
+-------+---------+------------------------------+----------------------------------------------+-----------+-----------+

lxc 컨테이너 생성하기

lxc launch <image>:<version> <container_name>
onam@Onam:/mnt/c/Users/kon64$ lxc launch ubuntu:24.04 d-registry
Creating d-registry
Starting d-registry
onam@Onam:/mnt/c/Users/kon64$ lxc list
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
|    NAME    |  STATE  |             IPV4             |                     IPV6                     |   TYPE    | SNAPSHOTS |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| d-registry | RUNNING | 10.64.213.236 (eth0)         | fd42:b762:7d5:b2f8:216:3eff:febd:b15f (eth0) | CONTAINER | 0         |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| dev-1      | RUNNING | 172.18.0.1 (docker_gwbridge) | fd42:b762:7d5:b2f8:216:3eff:fe25:4931 (eth0) | CONTAINER | 0         |
|            |         | 172.17.0.1 (docker0)         |                                              |           |           |
|            |         | 10.64.213.81 (eth0)          |                                              |           |           |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| dev-2      | RUNNING | 172.18.0.1 (docker_gwbridge) | fd42:b762:7d5:b2f8:216:3eff:fe91:7df9 (eth0) | CONTAINER | 0         |
|            |         | 172.17.0.1 (docker0)         |                                              |           |           |
|            |         | 10.64.213.67 (eth0)          |                                              |           |           |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+

새로운 lxc 컨테이너 d-registry 가 생겼습니다.

새로 생성된 lxc 컨테이너에 접속하기(/bin/bash)

lxc exec <lxc_container_name> -- /bin/bash

위 명령어를 통해 root 계정으로 해당 컨테이너에 들어갈 수 있습니다.

onam@Onam:/mnt/c/Users/kon64$ lxc exec d-registry -- /bin/bash
root@d-registry:~#

명령어 사용 후 유저가 root로 바뀌고 해당 컨테이너로 들어갔음을 확인 가능합니다.

컨테이너 유저 생성

adduser <user_name>

위 명령어를 통해 해당 컨테이너에 접속 가능한 유저를 만들 수 있고 이 유저별로 ssh 관련 파일을 설정할 수 있습니다.

root@d-registry:/home# adduser onam
info: Adding user `onam' ...
info: Selecting UID/GID from range 1000 to 59999 ...
info: Adding new group `onam' (1001) ...
info: Adding new user `onam' (1001) with group `onam (1001)' ...
info: Creating home directory `/home/onam' ...
info: Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for onam
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] Y
info: Adding new user `onam' to supplemental / extra groups `users' ...
info: Adding user `onam' to group `users' ...

몇가지 설정을 위해 이름 번호 등을 저장할 수 있는데 엔터를 바로 치면 스킵할 수 있습니다.

생성된 유저 ssh 설정하기

root@d-registry:/home# ll
total 16
drwxr-xr-x  4 root   root   4096 Dec 19 13:22 ./
drwxr-xr-x 21 root   root   4096 Nov 19 08:54 ../
drwxr-x---  2 onam   onam   4096 Dec 19 13:22 onam/
drwxr-x---  3 ubuntu ubuntu 4096 Dec 19 13:14 ubuntu/

ls -al (ll) 명령어를 통해 새로 만든 유저의 디렉토리가 생성되었음을 확인할 수 있습니다.

또한 root root 처럼 두가지 이름이 나타나는데 여기서 첫번째 root는 파일(디렉토리)의 owner 를 나타내고 두번째 root 는 group 을 나타냅니다.

ssh 접속시 사용하는 유저 이름과도 연관이 있으니 주의해야 합니다.

나중에 기존 유저의 디렉토리를 단순 복사해서 새로운 유저를 만들 때는 아래와 같은 작업을 통해 파일 권한과 owner 등을 수정해 줘야 합니다.

유저 파일 권한, owner 및 ssh 키 설정

root@d-registry:/home/onam# cd /home/onam/.ssh
root@d-registry:/home/onam/.ssh# vim authorized_keys
root@d-registry:/home/onam/.ssh# chmod 700 /home/onam/.ssh
root@d-registry:/home/onam/.ssh# chmod 600 /home/onam/.ssh/authorized_keys
root@d-registry:/home/onam/.ssh# chown -R onam:onam /home/onam/.ssh
root@d-registry:/home/onam/.ssh# ls -al
total 12
drwx------ 2 onam onam 4096 Dec 19 13:28 .
drwxr-x--- 4 onam onam 4096 Dec 19 13:28 ..
-rw------- 1 onam onam   88 Dec 19 13:25 authorized_keys

authorized_keys 파일에는 해당 유저가 사용할 ssh key 의 public 키를 넣고 저장하면 됩니다.

ssh-keygen 명령어를 통해 생성 가능합니다.

생성한 컨테이너 ssh 접속하기

ssh <user_name>@<IP_ADDRESS>

위 명령어를 통해 원하는 방금 설정을 마친 컨테이너에 접속이 가능합니다.

root@d-registry:/home/onam/.ssh# exit
exit
onam@Onam:/mnt/c/Users/kon64$ lxc list
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
|    NAME    |  STATE  |             IPV4             |                     IPV6                     |   TYPE    | SNAPSHOTS |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
| d-registry | RUNNING | 10.64.213.236 (eth0)         | fd42:b762:7d5:b2f8:216:3eff:febd:b15f (eth0) | CONTAINER | 0         |
+------------+---------+------------------------------+----------------------------------------------+-----------+-----------+
onam@Onam:/mnt/c/Users/kon64$ ssh onam@10.64.213.236
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)

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

 System information as of Thu Dec 19 13:47:22 UTC 2024

  System load:           0.0
  Usage of /:            1.0% of 1006.85GB
  Memory usage:          0%
  Swap usage:            0%
  Processes:             22
  Users logged in:       0
  IPv4 address for eth0: 10.64.213.236
  IPv6 address for eth0: fd42:b762:7d5:b2f8:216:3eff:febd:b15f


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update

Last login: Thu Dec 19 13:47:22 2024 from 10.64.213.1

하지만 IP를 기억하고 있기엔 귀찮기 때문에 config 파일을 사용해 IP 대신 컨테이너 이름으로 대체해서 접속할 수 있습니다.

onam@Onam:/mnt/c/Users/kon64$ vim ~/.ssh/config

위 명령어로 설정 파일을 열고 아래 내용을 본인에 맞게 수정 후 저장하면 됩니다.

Host <your_lxc_container_name>
    HostName <your_lxc_container_ip>
    User <your_ssh_user_name>
    IdentityFile ~/.ssh/<your_ssh_private_key_file>
ssh <your_ssh_user_name>@<lxc_container_name>

위 명령어를 사용해 접속할 수 있습니다.

onam@Onam:/mnt/c/Users/kon64$ ssh onam@d-registry
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)

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

System information as of Thu Dec 19 13:53:43 UTC 2024

 System load:           0.0
 Usage of /:            1.0% of 1006.85GB
 Memory usage:          0%
 Swap usage:            0%
 Processes:             22
 Users logged in:       0
 IPv4 address for eth0: 10.64.213.236
 IPv6 address for eth0: fd42:b762:7d5:b2f8:216:3eff:febd:b15f


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update

Last login: Thu Dec 19 13:47:34 2024 from 10.64.213.1
profile
권오남 / Onam Kwon

0개의 댓글