🐧 EC2에 λ°°ν¬ν•˜λ©΄μ„œ λͺ¨λ“  DevOps μ΄ˆλ³΄μžκ°€ λ°°μš°λŠ” Linux λͺ…λ Ήμ–΄

λ°œν–‰: (2026λ…„ 1μ›” 4일 μ˜€ν›„ 02:23 GMT+9)
5 λΆ„ μ†Œμš”
원문: Dev.to

Source: Dev.to

μ£„μ†‘ν•©λ‹ˆλ‹€λ§Œ, ν˜„μž¬ μ €λŠ” μ™ΈλΆ€ μ›Ήμ‚¬μ΄νŠΈμ˜ λ‚΄μš©μ„ 직접 κ°€μ Έμ˜¬ 수 μ—†μŠ΅λ‹ˆλ‹€. λ²ˆμ—­μ΄ ν•„μš”ν•œ λ³Έλ¬Έ ν…μŠ€νŠΈλ₯Ό μ—¬κΈ° μ±„νŒ…μ— λΆ™μ—¬ μ£Όμ‹œλ©΄, μš”μ²­ν•˜μ‹  λŒ€λ‘œ ν•œκ΅­μ–΄λ‘œ λ²ˆμ—­ν•΄ λ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€.

πŸ” SSH & Remote Access

ssh -i DevOps.pem ec2-user@ec2-x-x-x-x.compute-1.amazonaws.com

Purpose: EC2 μΈμŠ€ν„΄μŠ€μ— μ•ˆμ „ν•˜κ²Œ μ—°κ²°ν•©λ‹ˆλ‹€.

  • ssh – Secure Shell
  • -i DevOps.pem – 인증에 μ‚¬μš©ν•  개인 ν‚€
  • ec2-user@host – 둜그인 μ‚¬μš©μž + EC2 호슀트λͺ…

Running a command without interactive login

ssh user@host "command"

Purpose: 원격 EC2 μΈμŠ€ν„΄μŠ€μ—μ„œ λͺ…령을 직접 μ‹€ν–‰ν•©λ‹ˆλ‹€ (CI/CD νŒŒμ΄ν”„λΌμΈμ— 유용).

Example

ssh ec2-user@EC2_HOST "whoami && hostname"

Managing known hosts for non‑interactive CI/CD

ssh-keyscan -H EC2_HOST >> ~/.ssh/known_hosts
  • β€œAre you sure you want to continue connecting?” ν”„λ‘¬ν”„νŠΈλ₯Ό λ°©μ§€ν•©λ‹ˆλ‹€.
  • μžλ™ν™” μŠ€ν¬λ¦½νŠΈμ— ν•„μš”ν•©λ‹ˆλ‹€.

보닀 μ•ˆμ „ν•œ λ³€ν˜• (νƒ€μž„μ•„μ›ƒμ„ μΆ”κ°€ν•˜κ³  νŒŒμ΄ν”„λΌμΈμ΄ μ‹€νŒ¨ν•˜μ§€ μ•Šλ„λ‘ 함):

ssh-keyscan -T 10 -H EC2_HOST >> ~/.ssh/known_hosts || true

πŸ“ 파일 μ‹œμŠ€ν…œ 탐색 및 검사

pwd                # Print current directory
ls                 # List files and directories
ls -l              # Long format (permissions, owner, size)
ls -a              # Include hidden files
cd                 # Change directory

μ˜ˆμ‹œ

cd DevOpsWeb

πŸ”‘ 파일 및 디렉터리 κΆŒν•œ (맀우 μ€‘μš”)

κΆŒν•œ λ³€κ²½

chmod 755 directory
chmod 644 file
  • 7 = 읽기 +β€―μ“°κΈ°β€―+β€―μ‹€ν–‰ (μ†Œμœ μž)
  • 5 = 읽기 +β€―μ‹€ν–‰ (κ·Έλ£Ή/기타)
  • 4 = 읽기 μ „μš©

일반적인 μ‚¬μš©

  • Nginxκ°€ νŒŒμΌμ„ 읽을 수 μžˆλ„λ‘ ν—ˆμš©
  • 403β€―Forbidden 였λ₯˜ λ°©μ§€

μ†Œμœ κΆŒ λ³€κ²½

sudo chown -R ec2-user:nginx /home/ec2-user/DevOpsWeb
  • μ†Œμœ μž β†’ ec2-user
  • κ·Έλ£Ή β†’ nginx
  • -R β†’ μž¬κ·€μ 

루트 κΆŒν•œμœΌλ‘œ λͺ…λ Ή μ‹€ν–‰

sudoλŠ” 관리 κΆŒν•œμœΌλ‘œ λͺ…령을 μ‹€ν–‰ν•˜λ©° λ‹€μŒμ— ν•„μš”ν•©λ‹ˆλ‹€:

  • νŒ¨ν‚€μ§€ μ„€μΉ˜
  • μ‹œμŠ€ν…œ ꡬ성 νŽΈμ§‘
  • μ„œλΉ„μŠ€ μž¬μ‹œμž‘

🌐 Nginx (μ›Ή μ„œλ²„)

sudo yum install nginx -y          # Install
sudo systemctl start nginx        # Start service
sudo systemctl enable nginx        # Enable on boot
sudo systemctl status nginx       # Check status
sudo nginx -t                     # Test configuration (run before reload)
sudo systemctl reload nginx       # Reload config without downtime

🌍 λ„€νŠΈμ›Œν‚Ή 및 디버깅

curl http://localhost               # Test local web server response
curl http://PUBLIC_IP               # Test public access from the instance
lsof -i :80                         # See what process is using port 80

curl http://localhostκ°€ μ •μƒμ μœΌλ‘œ λ™μž‘ν•˜μ§€λ§Œ λΈŒλΌμš°μ €μ—μ„œ μ‚¬μ΄νŠΈμ— μ ‘κ·Όν•  수 μ—†λŠ” 경우, λ‹€μŒμ„ ν™•μΈν•˜μ„Έμš”:

  • λ³΄μ•ˆ κ·Έλ£Ή κ·œμΉ™
  • μΈμŠ€ν„΄μŠ€ λ°©ν™”λ²½ μ„€μ •

πŸ“¦ νŒ¨ν‚€μ§€ 관리 (Amazon Linux)

sudo yum install  -y       # νŒ¨ν‚€μ§€λ₯Ό λΉ„λŒ€ν™”μ‹μœΌλ‘œ μ„€μΉ˜
sudo yum remove  -y        # νŒ¨ν‚€μ§€ 제거

μ˜ˆμ‹œ

sudo yum install httpd -y
sudo yum install docker -y
sudo yum remove httpd -y            # 포트 μΆ©λŒμ„ λ°©μ§€ν•˜κΈ° μœ„ν•΄ Apache 제거

🐳 Docker κΈ°λ³Έ (μ§€κΈˆκΉŒμ§€)

docker run hello-world                     # Verify Docker installation
docker build -t devops-website .           # Build image from Dockerfile
docker ps -a                               # List all containers
docker exec -it <container> <command>      # Run a command inside a running container

μ˜ˆμ‹œ

docker exec -it devopsweb ls /usr/share/nginx/html

ec2-userκ°€ sudo 없이 Dockerλ₯Ό μ‹€ν–‰ν•˜λ„λ‘ ν—ˆμš©ν•©λ‹ˆλ‹€:

sudo usermod -aG docker ec2-user
# Log out and back in for the change to take effect

8️⃣ Rsync (CI/CD Deployment)

rsync -avz --delete source/ user@EC2_HOST:/path/to/dest/
  • -a – μ•„μΉ΄μ΄λΈŒ λͺ¨λ“œ (κΆŒν•œ, νƒ€μž„μŠ€νƒ¬ν”„ 등을 보쑴)
  • -v – μžμ„Έν•œ 좜λ ₯
  • -z – μ••μΆ•
  • --delete – λŒ€μƒμ— μ‘΄μž¬ν•˜μ§€λ§Œ μ†ŒμŠ€μ— 더 이상 μ—†λŠ” νŒŒμΌμ„ μ‚­μ œ

μ›ν•˜μ§€ μ•ŠλŠ” 파일 μ œμ™Έ

--exclude='.git*'
--exclude='.github/'

λŒ€μƒμ— κΆŒν•œ κ°•μ œ 적용

--chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r

이 μ˜΅μ…˜λ“€μ€ 배포 쀑 λ°œμƒν•œ exit codeβ€―23 및 κΆŒν•œ κ΄€λ ¨ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

πŸ§ͺ GitHub Actions / CI λͺ…λ Ήμ–΄

set -e          # Exit immediately if a command fails
set -eux        # -e: exit on error, -u: treat unset variables as error, -x: print commands (debug)
mkdir -p ~/.ssh
echo "$SECRET" > ~/.ssh/id_rsa   # Write SSH private key from GitHub Secrets

μ΄λŸ¬ν•œ 섀정은 CI νŒŒμ΄ν”„λΌμΈμ΄ 크게 그리고 λͺ…ν™•ν•˜κ²Œ μ‹€νŒ¨ν•˜λ„λ‘ ν•˜μ—¬, 문제λ₯Ό 쑰기에 포착할 수 있게 ν•©λ‹ˆλ‹€.

Back to Blog

κ΄€λ ¨ κΈ€

더 보기 Β»

Dockerκ°€ λ‚΄ Linux λ°μŠ€ν¬ν†±μ—μ„œμ˜ 고좩을 ν•΄κ²°ν–ˆλ‹€....

μ΅œκ·Όμ— Dockerλ₯Ό 배우기 μ‹œμž‘ν–ˆκ³ , μ‹€μ œλ‘œ μ–΄λ–»κ²Œ μž‘λ™ν•˜λŠ”μ§€β€”Docker Engine이 μ–΄λ–»κ²Œ λ™μž‘ν•˜λŠ”μ§€, μ»¨ν…Œμ΄λ„ˆκ°€ μ–΄λ–»κ²Œ κ²©λ¦¬λ˜λŠ”μ§€ 등을 μ΄ν•΄ν•˜λ €κ³  ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€β€”...

μ‚°μ—… ν˜„μž₯의 예츑 μœ μ§€λ³΄μˆ˜: μ΅œλŒ€ 가동 μ‹œκ°„μ„ μœ„ν•œ IoT 및 DevOps μ‹€μ²œ ν™œμš©

ν˜„λŒ€ μ‚°μ—…μ—μ„œ 기계λ₯Ό 연속 가동 μƒνƒœλ‘œ μœ μ§€ν•˜λŠ” 것은 큰 도전 κ³Όμ œμž…λ‹ˆλ‹€. μ‹œμŠ€ν…œμ€ λ§€λ…„ 더 λ³΅μž‘ν•΄μ§€μ§€λ§Œ, 신뒰성에 λŒ€ν•œ κΈ°λŒ€λŠ” κ²°μ½” μ™„ν™”λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. Predicti...

AIλŠ” μƒˆλ‘œμš΄ μ£Όλ‹ˆμ–΄ μ—”μ§€λ‹ˆμ–΄λ‹€

μ‹€μ œ AI μ„±κ³ΌλŠ” κ³ΌλŒ€κ΄‘κ³ λ‚˜ νŒŒμΌλŸΏμ—μ„œ λ‚˜μ˜€λŠ” 것이 μ•„λ‹ˆλ‹€. 이미 κ°•λ ₯ν•œ ꡐ윑, λ©˜ν† λ§ 및 μ—”μ§€λ‹ˆμ–΄λ§ λ¬Έν™”λ₯Ό κ°–μΆ˜ νŒ€μ—μ„œ λ‚˜μ˜¨λ‹€....