← 가이드 목록으로 ← Back to guides

Linux 자동화와 보안 설정 완벽 가이드 Linux Automation & Security Configuration Guide

시스템 관리의 기본을 익혔다면, 이제 반복 작업 자동화보안 강화를 배울 차례입니다. cron으로 작업을 스케줄링하고, 방화벽과 SSH 설정으로 서버를 보호하세요.

After learning system administration basics, it's time to learn task automation and security hardening. Schedule tasks with cron and protect your server with firewall and SSH configurations.

Cron 작업 스케줄링 Cron Job Scheduling

cron은 지정된 시간에 자동으로 명령어를 실행하는 스케줄러입니다. 백업, 로그 정리, 모니터링 등 반복 작업에 필수입니다.

cron is a scheduler that automatically runs commands at specified times. Essential for backup, log cleanup, monitoring, and other repetitive tasks.

⏰ Cron 표현식 형식 ⏰ Cron Expression Format

Min Hour Day Month 요일 Weekday
0-59 0-23 1-31 1-12 0-7
# crontab 편집
$ crontab -e

# 현재 crontab 확인
$ crontab -l

# crontab 예시
# 매일 새벽 2시에 백업 실행
0 2 * * * /home/user/scripts/backup.sh

# 매주 월요일 오전 9시에 리포트 전송
0 9 * * 1 /home/user/scripts/weekly-report.sh

# 5분마다 헬스체크 실행
*/5 * * * * /home/user/scripts/healthcheck.sh

# 매월 1일 자정에 로그 정리
0 0 1 * * /home/user/scripts/cleanup-logs.sh
# Edit crontab
$ crontab -e

# View current crontab
$ crontab -l

# Crontab examples
# Run backup daily at 2 AM
0 2 * * * /home/user/scripts/backup.sh

# Send report every Monday at 9 AM
0 9 * * 1 /home/user/scripts/weekly-report.sh

# Run healthcheck every 5 minutes
*/5 * * * * /home/user/scripts/healthcheck.sh

# Clean logs on 1st of every month at midnight
0 0 1 * * /home/user/scripts/cleanup-logs.sh

💡 systemd timer 대안 💡 systemd timer Alternative

최신 시스템에서는 systemd timer도 많이 사용됩니다. systemctl list-timers로 타이머 목록을 확인하세요.

Modern systems often use systemd timer. Check timer list with systemctl list-timers.

방화벽 설정 (UFW / firewalld) Firewall Configuration (UFW / firewalld)

UFW (Ubuntu) UFW (Ubuntu)

# UFW 활성화
# ufw enable

# 상태 확인
# ufw status verbose

# SSH 포트 허용 (필수!)
# ufw allow 22/tcp
# ufw allow ssh

# 웹 서버 포트 허용
# ufw allow 80/tcp
# ufw allow 443/tcp

# 특정 IP만 허용
# ufw allow from 192.168.1.100 to any port 3306

# 규칙 삭제
# ufw delete allow 80/tcp
# Enable UFW
# ufw enable

# Check status
# ufw status verbose

# Allow SSH port (essential!)
# ufw allow 22/tcp
# ufw allow ssh

# Allow web server ports
# ufw allow 80/tcp
# ufw allow 443/tcp

# Allow specific IP only
# ufw allow from 192.168.1.100 to any port 3306

# Delete rule
# ufw delete allow 80/tcp

firewalld (RHEL/CentOS) firewalld (RHEL/CentOS)

# 상태 확인
# firewall-cmd --state

# 포트 열기 (영구적)
# firewall-cmd --permanent --add-port=80/tcp
# firewall-cmd --reload

# 서비스로 열기
# firewall-cmd --permanent --add-service=http

# 현재 규칙 확인
# firewall-cmd --list-all
# Check status
# firewall-cmd --state

# Open port (permanent)
# firewall-cmd --permanent --add-port=80/tcp
# firewall-cmd --reload

# Open by service
# firewall-cmd --permanent --add-service=http

# List current rules
# firewall-cmd --list-all

⚠️ 방화벽 설정 주의 ⚠️ Firewall Configuration Caution

방화벽 활성화 전에 반드시 SSH 포트를 허용하세요! 그렇지 않으면 서버에 접속할 수 없게 됩니다.

Always allow SSH port before enabling firewall! Otherwise you'll be locked out of the server.

SSH 보안 강화 SSH Security Hardening

# /etc/ssh/sshd_config 수정

# 루트 로그인 비활성화
PermitRootLogin no

# 비밀번호 인증 비활성화 (키 인증만 사용)
PasswordAuthentication no

# 포트 변경 (기본 22에서 변경)
Port 2222

# 허용할 사용자 지정
AllowUsers deploy admin

# 설정 적용
# systemctl restart sshd
# Edit /etc/ssh/sshd_config

# Disable root login
PermitRootLogin no

# Disable password authentication (key auth only)
PasswordAuthentication no

# Change port (from default 22)
Port 2222

# Specify allowed users
AllowUsers deploy admin

# Apply settings
# systemctl restart sshd

SSH 키 인증 설정 SSH Key Authentication Setup

# 로컬에서 SSH 키 생성
$ ssh-keygen -t ed25519 -C "[email protected]"

# 공개키를 서버에 복사
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

# 또는 수동으로 복사
$ cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# Generate SSH key locally
$ ssh-keygen -t ed25519 -C "[email protected]"

# Copy public key to server
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

# Or copy manually
$ cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

자동화 쉘 스크립트 Automation Shell Scripts

#!/bin/bash
# backup.sh - 자동 백업 스크립트

DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backup"
SOURCE="/var/www/html"

# 백업 디렉토리 생성
mkdir -p $BACKUP_DIR

# tar로 압축
tar -czf $BACKUP_DIR/backup_$DATE.tar.gz $SOURCE

# 7일 이상 된 백업 삭제
find $BACKUP_DIR -name "backup_*.tar.gz" -mtime +7 -delete

# 결과 로깅
echo "[$DATE] Backup completed" >> /var/log/backup.log

💡 스크립트 실행 권한 💡 Script Execution Permission

스크립트 파일에 실행 권한을 부여하세요: chmod +x backup.sh

Grant execution permission to the script: chmod +x backup.sh