본문 바로가기

리눅스

우분투 24.04에서 PXE 자동 설치 서버를 구축하는 방법

반응형

우분투 24.04에서 PXE 자동 설치 서버를 구축하는 방법

필수 패키지 설치

sudo apt update
sudo apt install -y curl whois

1. DHCP 서버(isc-dhcp-server)

패키지 설치 및 서비스 활성화

sudo apt install -y isc-dhcp-server
sudo systemctl enable --now isc-dhcp-server

설정 파일

sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf_$(date '+%Y%m%d_%H%M%S')
sudo tee /etc/dhcp/dhcpd.conf > /dev/null << 'EOF'
option domain-name "scbyun.com";
option domain-name-servers 8.8.8.8, 8.8.4.4;

default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;

subnet 192.168.0.0 netmask 255.255.255.0 {
    range 192.168.0.206 192.168.0.210;
    option routers 192.168.0.1;
    option subnet-mask 255.255.255.0;
    option broadcast-address 192.168.0.255;
    # PXE 설정
    next-server 192.168.0.201;
    filename "pxelinux.0";
}
EOF

설정 파일 검사

sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf

서비스 재시작

sudo systemctl restart isc-dhcp-server

서비스 상태 확인

sudo systemctl status isc-dhcp-server

2. TFTP 서버(tftpd-hpa)

패키지 설치 및 서비스 활성화

sudo apt install -y tftpd-hpa
sudo systemctl enable --now tftpd-hpa

설정 파일

sudo cp /etc/default/tftpd-hpa /etc/default/tftpd-hpa_$(date '+%Y%m%d_%H%M%S')
sudo tee /etc/default/tftpd-hpa > /dev/null << 'EOF'
# /etc/default/tftpd-hpa

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="--secure --blocksize 512"
EOF

TFTP 루트 디렉토리 생성

sudo mkdir -p /var/lib/tftpboot
sudo chmod -R 755 /var/lib/tftpboot
sudo chown -R tftp:tftp /var/lib/tftpboot

서비스 재시작

sudo systemctl restart tftpd-hpa

서비스 상태 확인

sudo systemctl status tftpd-hpa

3. Nginx 설치

Nginx 공식 저장소 등록

sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginx

4. PXELINUX(Syslinux) 구성(BIOS 모드)

sudo apt install -y pxelinux syslinux-common

PXELINUX 부팅 이미지(pxelinux.0)를 /var/lib/tftpboot 디렉터리로 복사

sudo cp /usr/lib/PXELINUX/pxelinux.0 /var/lib/tftpboot/
sudo cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libcom32.c32,libutil.c32,menu.c32,vesamenu.c32} \
	/var/lib/tftpboot/

UEFI 모드

더보기

---

sudo apt install -y syslinux-efi
sudo mkdir -p /var/lib/tftpboot/uefi
sudo cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi /var/lib/tftpboot/uefi
sudo cp /usr/lib/syslinux/modules/efi64/{ldlinux.e64,libcom32.c32,libutil.c32,vesamenu.c32} \
	/var/lib/tftpboot/uefi
ln -s /var/lib/tftpboot/pxelinux.cfg /var/lib/tftpboot/uefi/pxelinux.cfg

---

TFTP 루트 디렉토리 설정

sudo chmod -R 755 /var/lib/tftpboot
sudo chown -R tftp:tftp /var/lib/tftpboot
728x90

우분투 24.04 파일 준비(HTTP 제공)

우분투 24.04의 Netboot 커널(vmlinuz)과 램디스크(initrd) 파일을 다운로드하여 TFTP 디렉토리에 복사합니다.

sudo tee /etc/nginx/nginx.conf << 'EOF'
### nginx.conf
user  www-data www-data;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    # 성능 설정
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    client_max_body_size 0;
    client_body_timeout 300s;
    client_header_timeout 300s;
    send_timeout 300s;

    gzip  off;

    include /etc/nginx/conf.d/*.conf;
}

EOF

default.conf 설정 파일 백업

sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf_$(date '+%Y%m%d_%H%M%S')

default.conf 설정 파일 수정

sudo tee /etc/nginx/conf.d/default.conf << 'EOF'
server {
    listen 80 reuseport fastopen=3;
    server_name _;

    access_log /var/log/nginx/pxe-access.log combined buffer=32k flush=5s;
    error_log /var/log/nginx/pxe-error.log warn;

    # 루트 경로
    root /usr/share/nginx/html;

    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;

        # 디렉토리 리스팅 스타일
        add_header Content-Type text/html;
    }

    location /iso/ {
        alias /usr/share/nginx/html/iso/;
        autoindex on;

        # ISO 파일 특별 처리
        location ~ \.iso$ {
            add_header Content-Type application/octet-stream;
            add_header Accept-Ranges bytes;
            # 속도 최적화
            directio 4m;
            directio_alignment 512;
            output_buffers 2 1m;
        }
    }

    location /ubuntu/ {
        alias /usr/share/nginx/html/ubuntu/;
        autoindex on;

        # 부팅 파일들
        location ~ \.(vmlinuz|initrd|initrd\.gz|linuz)$ {
            add_header Content-Type application/octet-stream;
            add_header Cache-Control "public, max-age=86400";
            # 메모리에 캐시
            open_file_cache max=1000 inactive=20s;
            open_file_cache_valid 30s;
            open_file_cache_min_uses 2;
            open_file_cache_errors on;
        }
    }

    location /autoinstall/ {
        alias /usr/share/nginx/html/autoinstall/;
        autoindex on;

        # cloud-init 파일
        location ~ \.(yaml|yml|json|txt)$ {
            default_type text/plain;
            charset utf-8;
            add_header Content-Type "text/plain; charset=utf-8";
        }

        # user-data 특별 처리
        location = /autoinstall/user-data {
            default_type text/plain;
            charset utf-8;
            add_header Content-Type "text/plain; charset=utf-8";
        }

        location = /autoinstall/meta-data {
            default_type text/plain;
            return 200 "instance-id: nocloud\nlocal-hostname: ubuntu\n";
        }
    }

    # PXE 부팅 파일들 (TFTP 대신 HTTP 부팅용)
    location /tftp/ {
        alias /var/lib/tftpboot/;
        autoindex on;

        location ~ \.(0|pxe|cfg|32|com32)$ {
            add_header Content-Type application/octet-stream;
        }
    }

    # 상태 페이지 (선택사항)
    location /status {
        stub_status on;
        access_log off;
        allow 192.168.0.0/24;
        deny all;
    }
}
EOF
sudo nginx -t
sudo systemctl restart nginx

우분투 ISO 파일 준비

sudo mkdir -p /usr/share/nginx/html/{iso,ubuntu/24.04}

우분투 24.04 네트부팅 파일 다운로드

cd /usr/share/nginx/html/iso
wget https://releases.ubuntu.com/noble/ubuntu-24.04.3-live-server-amd64.iso

임시 마운트 디렉토리 생성 및 ISO 마운트

sudo mount -o loop,ro ubuntu-24.04.3-live-server-amd64.iso /mnt
sudo cp /mnt/casper/{vmlinuz,initrd} /var/lib/tftpboot/
sudo cp -r /mnt/* /usr/share/nginx/html/ubuntu/24.04/
sudo cp -r /mnt/.disk /usr/share/nginx/html/ubuntu/24.04/
sudo umount /mnt
sudo chown -R www-data:www-data /usr/share/nginx/html/ubuntu
cd /usr/share/nginx/html/ubuntu/24.04/casper
ln -s ubuntu-server-minimal.ubuntu-server.installer.squashfs filesystem.squashfs

PXE 설정 파일 생성

sudo mkdir -p /var/lib/tftpboot/pxelinux.cfg
sudo tee /var/lib/tftpboot/pxelinux.cfg/default > /dev/null << 'EOF'
# /var/lib/tftpboot/pxelinux.cfg/default 수정
DEFAULT menu.c32
PROMPT 0
TIMEOUT 300
ONTIMEOUT local-boot
MENU TITLE Ubuntu 24.04 PXE Install

LABEL ubuntu-http
    MENU LABEL ^1 - Ubuntu 24.04 HTTP Install
    KERNEL http://192.168.0.201/ubuntu/vmlinuz
    INITRD http://192.168.0.201/ubuntu/initrd
    APPEND ip=dhcp url=http://192.168.0.201/iso/ubuntu-24.04.3-live-server-amd64.iso autoinstall ds=nocloud-net;s=http://192.168.0.201/autoinstall/ console=tty0 console=ttyS0,115200n8
    IPAPPEND 2

LABEL ubuntu-tftp
    MENU LABEL ^2 - Ubuntu 24.04 TFTP Install
    KERNEL vmlinuz
    INITRD initrd
    APPEND ip=dhcp root=/dev/ram0 ramdisk_size=1500000 url=http://192.168.0.201/iso/ubuntu-24.04.3-live-server-amd64.iso nomodeset
    IPAPPEND 2

LABEL ubuntu-text
    MENU LABEL ^3 - Ubuntu 24.04 TFTP Install(Text Mode)
    KERNEL vmlinuz
    INITRD initrd
    APPEND ip=dhcp root=/dev/ram0 ramdisk_size=1500000 url=http://192.168.0.201/iso/ubuntu-24.04.3-live-server-amd64.iso autoinstall ds=nocloud-net;s=http://192.168.0.201/autoinstall/ cloud-config-url=/dev/null console=ttyS0,115200n8 nomodeset text debug
    IPAPPEND 2

LABEL local-boot
    MENU LABEL ^4 - Boot from Local Disk
    LOCALBOOT 0

EOF

Nginx 웹 서버 구성(Cloud-Init 파일 제공)

디렉토리 구조 생성

sudo mkdir -p /usr/share/nginx/html/autoinstall
sudo chown -R www-data:www-data /usr/share/nginx/html/autoinstall
sudo systemctl restart nginx

Autoinstall 설정(User-Data)

Autoinstall 설정 파일인 user-data와 meta-data를 Nginx 웹 루트에 생성합니다.

 

Vendor-Date 파일 생성

sudo touch /usr/share/nginx/html/autoinstall/vendor-data

Meta-Data 파일 생성

sudo tee /usr/share/nginx/html/autoinstall/meta-data > /dev/null << 'EOF'
instance-id: autoinstalled-server-001
local-hostname: autoinstalled-server-001
EOF

User-Data 파일 생성

  • ubuntu / ubuntu
# 패스워드 : ubuntu1!
openssl passwd -6 'ubuntu1!'
sudo tee /usr/share/nginx/html/autoinstall/user-data > /dev/null << 'EOF'
#cloud-config
autoinstall:
  version: 1
  identity:
    hostname: autoinstalled-server-001
    password: "$6$7tUafIA6FEeUzS59$8ES67iWLiFd3fVyoAKQnrOqUm4T4qigAuH20tOJA8NjZYHBCouFIQVyIf4yYqxmAQqvUDr6zex1e0By01f0el1"
    username: ubuntu

  locale: ko_KR.UTF-8
  
  keyboard:
    layout: us

  network:
    version: 2
    ethernets:
      eth0:
        dhcp4: true

  # 저장소 설정        
  storage:
    layout:
      name: lvm

  ssh:
    install-server: true
    allow-pw: true
  
  # 패키지 목록
  packages:
    - net-tools
 
  late-commands:
    - sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /target/etc/ssh/sshd_config
EOF

서비스 재시작

sudo systemctl restart isc-dhcp-server tftpd-hpa nginx

웹 서버 접근 확인

curl -I http://192.168.0.201/autoinstall/user-data

서버에서 PXE 부팅(클라이언트 서버)

  1. F12 → Network Boot (PXE)
  2. 무인 설치 시작

설치 로그 확인 (디버깅)

설치 중 콘솔

Ctrl + Alt + F2

cloud-init 로그

/var/log/cloud-init.log
/var/log/cloud-init-output.log
/var/log/installer/subiquity-debug.log

 

참고URL

- ubuntu releases : Ubuntu 24.04.3 (Noble Numbat)

- Ubuntu Documentation : How to use cloud-init

- GitHub : canonical/cloud-init

 

728x90
반응형