리눅스 서버 구축
[최종] 리눅스 네트워크 서버 자동화 스크립트 정리
ahhyun98
2025. 5. 28. 16:50
지금까지 실습을 통해 구성한 리눅스 기반의 다양한 네트워크 서버를 Shell Script 자동화 스크립트로 문서화하는 프로젝트를 해보았다. 이 스크립트들은 서버 초기 설치 및 재설정 시 반복 사용이 가능하도록 작성하였다. 그리고 민감할 수 있는 IP 주소는 xx 처리하였다.
🐱 자동화 스크립트
1. Apache2 웹 서버
#!/bin/bash
# Apache2 설치 및 서비스 시작
apt update
apt install apache2 -y
systemctl enable apache2
systemctl start apache2
ufw allow "Apache Full" # 80(HTTP), 443(HTTPS) 포트 허용 (Apache :HTTP만 허용, Apache Secure : HTTPS만 허용 )
2. Nginx 웹 서버
#!/bin/bash
# Nginx 설치 및 기본 설정 적용
apt update
apt install nginx -y
mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
cat <<EOF > /etc/nginx/sites-available/default
server {
listen 80 default_server;
root /var/www/html;
index index.html;
server_name localhost;
location / {
try_files \$uri \$uri/ =404;
}
}
EOF
nginx -t
systemctl restart nginx
3. MariaDB
#!/bin/bash
# MariaDB 설치 및 사용자 계정 생성
apt update
apt install mariadb-server -y
mysql -u root <<EOF
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
systemctl enable mariadb
systemctl start mariadb
4. BIND DNS 서버
#!/bin/bash
# BIND DNS 서버 설치 및 기본 설정
apt update
apt install bind9 -y
cat <<EOF > /etc/bind/named.conf.options
options {
directory "/var/cache/bind";
recursion yes;
allow-query { any; };
forwarders { 8.8.8.8; };
listen-on port 53 { any; };
};
EOF
ufw allow 53
systemctl restart bind9
5. Samba 파일 공유
#!/bin/bash
# Samba 설치 및 공유 디렉토리 구성
apt update
apt install samba -y
mkdir /sambashare
chmod 777 /sambashare
useradd -M sambauser
(echo "password"; echo "password") | smbpasswd -a sambauser
cat <<EOF >> /etc/samba/smb.conf
[sambashare]
path = /sambashare
read only = no
valid users = sambauser
EOF
systemctl restart smbd
ufw allow samba
6. DHCP 서버
#!/bin/bash
# DHCP 서버 설치 및 범위 설정
apt update
apt install isc-dhcp-server -y
cat <<EOF > /etc/dhcp/dhcpd.conf
subnet 192.168.XXX.0 netmask 255.255.255.0 {
range 192.168.XXX.10 192.168.XXX.50;
option routers 192.168.XXX.1;
option domain-name-servers 8.8.8.8;
}
EOF
systemctl restart isc-dhcp-server
ufw allow 67/udp
7. VSFTPD (FTP 서버)
#!/bin/bash
# FTP 서버 설치 및 설정 수정
apt update
apt install vsftpd -y
sed -i 's/#write_enable=YES/write_enable=YES/' /etc/vsftpd.conf
sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' /etc/vsftpd.conf
systemctl restart vsftpd
ufw allow 21/tcp
8. SSH 서버
#!/bin/bash
# SSH 설치 및 root 접속 허용
apt update
apt install openssh-server -y
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl restart ssh
ufw allow 22
9. NFS 서버
#!/bin/bash
# NFS 서버 설치 및 디렉토리 공유
apt update
apt install -y nfs-kernel-server
mkdir /nfsshare
chmod 777 /nfsshare
echo "/nfsshare *(rw,sync,no_subtree_check)" >> /etc/exports
exportfs -a
ufw allow from any to any port nfs
ufw allow from any to any port 111
systemctl restart nfs-kernel-server
10. NFS 클라이언트
#!/bin/bash
# NFS 클라이언트 마운트 설정
apt update
apt install nfs-common -y
mkdir -p /mnt/nfs/xxx
mount -t nfs 192.168.XXX.XXX:/nfsshare /mnt/nfs/xxx
11. rsyslog 로그 수집기
#!/bin/bash
# rsyslog 설치 및 로그 실시간 확인
apt update
apt install rsyslog -y
systemctl restart rsyslog
tail -f /var/log/syslog
이 블로그는 불법 해킹 및 악의적인 활동을 지양하며, 그런 행위는 절대 권장하지 않습니다.
모든 실습은 허가된 환경에서만 진행해야 하며, 법적 책임은 사용자 본인에게 있습니다.