๐Ÿ ์šฐ๋ถ„ํˆฌ ํ™ˆ ์„œ๋ฒ„ ๊ตฌ์ถ•๊ธฐ - DDNS ์„ค์ •๐Ÿ 

๊น€ํƒœ์›ยท2022๋…„ 2์›” 22์ผ
3
post-thumbnail
post-custom-banner

๊ฐœ์š”


๋ณธ ์‹œ๋ฆฌ์ฆˆ๋Š” Ubuntu Server 20.04 ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” DDNS๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณผ์ •์„ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ค.


1. DDNS๋ž€

DDNS, Dynamic DNS๋Š” ์‹ค์‹œ๊ฐ„์œผ๋กœ DNS๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ฃผ๋กœ ๋„๋ฉ”์ธ์˜ IP๊ฐ€ ์œ ๋™์ ์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ๋œ๋‹ค. IP๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ DDNS๋กœ ์„ค์ •ํ•œ ๋„๋ฉ”์ธ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์šฉ์ดํ•˜๊ฒŒ ์ ‘์† ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ•„์ž์˜ ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜ ๊ฐ€์ •์˜ ์ธํ„ฐ๋„ท ํšŒ์„ ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์ธ IP ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๊ธฐ์— DDNS ์„ค์ •์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค.

๋งŒ์•ฝ AWS, GCP ๋“ฑ์˜ ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด DDNS ์„ค์ • ๊ณผ์ •์€ ์ƒ๋žตํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

ํ•„์ž๋Š” API๋ฅผ ์ด์šฉํ•ด DDNS๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Cloudflare๋ฅผ ์ด์šฉํ•  ์˜ˆ์ •์ด๋‹ค.

๋งŒ์ผ ๋ณธ์ธ์˜ ๋„๋ฉ”์ธ์— ์—ฐ๊ฒฐ๋œ DNS๊ฐ€ DDNS ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค๋ฉด ๊ทธ๊ฒƒ์„ ์ด์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.


2. Cloudflare

Cloudflare์— ๊ฐ€์ž…ํ•˜์ž.

๋ฌด๋ฃŒ ์š”๊ธˆ์ œ(Free Plan)์„ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.

Cloudflare ๊ฐ€์ž… ์ดํ›„์—๋Š” ๋ณธ์ธ ๋„๋ฉ”์ธ์„ Cloudflare์™€ ์—ฐ๊ฒฐํ•œ๋‹ค.

๋„๋ฉ”์ธ์„ ์—ฐ๊ฒฐํ•œ ์ดํ›„์—๋Š” DNS ๋ ˆ์ฝ”๋“œ์— ์ž์‹ ์ด ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ DNS ๋ ˆ์ฝ”๋“œ๋ฅผ ๋น ์ง์—†์ด ์ž…๋ ฅํ•œ๋‹ค.

๋กœ๊ทธ์ธ์ด ๋œ ์ƒํƒœ๋ผ๋ฉด Cloudflare API Key ํ™•์ธ ๋งํฌ์— ์ ‘์†ํ•ด Global API Key๋ฅผ ํ™•์ธํ•˜๊ณ  ๋ฉ”๋ชจํ•ด๋†“์ž.


3. DDNS ์Šคํฌ๋ฆฝํŠธ

nano ํŽธ์ง‘๊ธฐ๋ฅผ ์ด์šฉํ•ด ์‰˜ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์ž.
(vi๋ฅผ ์ด์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•จ)

sudo nano /usr/local/etc/ddns-cloudflare.sh

์•„๋ž˜ ์Šคํฌ๋ฆฝํŠธ ์ค‘ Login_Email, Global_API_Key, Domain, A_Record ํ•ญ๋ชฉ์€ ๋ณธ์ธ์˜ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

#!/bin/bash

### CloudFlare A Recoard Updater
### A_Record : Separate the contents with commas (,)
### Proxied : true or false
### TTL : Between 120 and 2147483647 seconds, or 1 for automatic
### To force updating, run with -f

Login_Email=id@example.com
Global_API_Key=hadbkv85sf68hgueaf89o9svgoidbki73o92
Domain=example.com
A_Record=example.com,sub.example.com
Proxied=true
TTL=1

[ ! -f /var/tmp/ip.txt ] && touch /var/tmp/ip.txt
CIP=$(curl -s "https://ipv4.icanhazip.com/")
PIP=$(cat /var/tmp/ip.txt)
echo -e "CloudFlare A Recoard Updater v1.0.9"
echo -e "Current IP: $CIP"
echo -e "Previous IP: $PIP"

if [ "$CIP" == "$PIP" ] && [[ $1 != "-f" ]]; then
  echo "No need to update"; exit 0
elif [[ $1 == "-f" ]]; then
  echo "Force updating A recoard......"
elif [ "$CIP" != "$PIP" ]; then
  echo "Updating A recoard......"
fi

echo $CIP > /var/tmp/ip.txt

V4="https://api.cloudflare.com/client/v4/zones"
H1="-HX-Auth-Email:$Login_Email"
H2="-HX-Auth-Key:$Global_API_Key"
H3="-HContent-Type:application/json"
ZN=$(curl -s -X GET "$V4?name=$Domain" \
    $H1 $H2 $H3 | grep -Po '(?<="id":")[^"]*' | head -1)

string=$A_Record
IFS=',' ARARY=(${string})

function AID() {
  for AREC in "${ARARY[@]}"
    do
      (curl -s -X GET "$V4/$ZN/dns_records?name=$AREC" \
      $H1 $H2 $H3 | grep -Po '(?<="id":")[^"]*' | head -1)
    done
}

IFS=$'\n' AIDARY=($(AID))

ATOTAL=${#ARARY[*]}

for ((i=0; i<$ATOTAL; i++))
  do
    (curl -s -X PUT "$V4/$ZN/dns_records/${AIDARY[$i]}" $H1 $H2 $H3 \
    --data "{\"type\":\"A\",\"name\":\"${ARARY[$i]}\",\"content\":\"$CIP\",\"proxied\":$Proxied,\"ttl\":$TTL}" \
    | grep -Po '(?<="name":")[^"]*|(?<="content":")[^"]*|(?<=Z"},)[^}]*|(?<="success":false,)[^$]*|(?<=\s\s)[^$]*' | xargs)
  done

Login_Email: CloudFlare์— ๋กœ๊ทธ์ธํ•  ๋•Œ ID๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
Global_API_Key: ์•ž์„œ ํ™•์ธํ•œ CloudFlare์˜ Global API Key๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
Domain: CloudFlare์— ๋“ฑ๋กํ•œ ๋„๋ฉ”์ธ์„ ์ž…๋ ฅํ•œ๋‹ค. 1๊ฐœ๋งŒ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
A_Record: Domain ํ•ญ๋ชฉ์— ์ž…๋ ฅํ•œ ๋„๋ฉ”์ธ์˜ ๋ฃจํŠธ ๋„๋ฉ”์ธ ๋ฐ ์„œ๋ธŒ๋„๋ฉ”์ธ์„ ๋ชจ๋‘ ๊ธฐ์ž…ํ•œ๋‹ค.
๊ฐ๊ฐ์˜ ๊ตฌ๋ถ„์€ ์‰ผํ‘œ ,๋กœ ํ•œ๋‹ค. ์ด ๋„๋ฉ”์ธ๋“ค์€ ๋ชจ๋‘ CloudFlare์˜ DNS ๋ ˆ์ฝ”๋“œ์— ๋จผ์ € ์ƒ์„ฑ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
Proxied: CloudFlare์˜ CDN, SSL ๋“ฑ์˜ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ ค๋ฉด true๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์€ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  DNS๋งŒ ์ด์šฉํ•˜๋ ค๋ฉด false๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
TTL: TTL ๊ฐ’์„ ์ดˆ ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•˜๋Š”๋ฐ, ์–ด๋Š ์‹œ์ ๋ถ€ํ„ฐ TTL์„ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ ์ž…๋ ฅํ•ด Auto๋กœ ์„ค์ •ํ•œ๋‹ค. ๋‹ค๋ฅธ ๊ฐ’์„ ์ž…๋ ฅํ•ด๋„ Auto๋กœ ์ ์šฉ๋œ๋‹ค.

์ด ์Šคํฌ๋ฆฝํŠธ๋Š” Domain ํ•ญ๋ชฉ์— 1๊ฐœ์˜ ๋„๋ฉ”์ธ๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋งŒ์ผ CloudFlare์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„๋ฉ”์ธ์„ ๋“ฑ๋กํ–ˆ๋‹ค๋ฉด ์ด ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

ํŽธ์ง‘์„ ๋งˆ์ณค์œผ๋ฉด Ctrlํ‚ค์™€ xํ‚ค๋ฅผ ๋™์‹œ์— ๋ˆŒ๋Ÿฌ์„œ nano ํŽธ์ง‘๊ธฐ๋ฅผ ๋น ์ ธ๋‚˜์˜ค๋ฉด์„œ ์ €์žฅํ•œ๋‹ค.

์Šคํฌ๋ฆฝํŠธ์— ์‹คํ–‰ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค.

sudo chmod u+x /usr/local/etc/ddns-cloudflare.sh

-f ์˜ต์…˜์œผ๋กœ ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์„œ CloudFlare์˜ DNS๋ฅผ ์ตœ์ดˆ๋กœ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

sudo /usr/local/etc/ddns-cloudflare.sh -f

-f ์˜ต์…˜์„ ๋ถ™์ด๋ฉด ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„์˜ IP ์ฃผ์†Œ ๋ณ€๊ฒฝ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ํ˜„์žฌ์˜ IP ์ฃผ์†Œ๋กœ CloudFlare์˜ DNS๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ , -f ์˜ต์…˜ ์—†์ด ์‹คํ–‰ํ•˜๋ฉด IP ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ์—๋งŒ CloudFlare์˜ DNS๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

์—…๋ฐ์ดํŠธ์— ์„ฑ๊ณตํ–ˆ์œผ๋ฉด example.com 123.123.123.123 success:true,errors:[],messages:[] ํ˜•ํƒœ์˜ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ , CloudFlare ๋Œ€์‹œ๋ณด๋“œ์—์„œ DNS ๋ ˆ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๋ฉด IP ์ฃผ์†Œ๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋‹ค์Œ, ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„์˜ ์ž‘์—… ์Šค์ผ€์ค„๋Ÿฌ์— ๋“ฑ๋กํ•ด์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ IP ์ฃผ์†Œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•˜๋„๋ก ์„ค์ •ํ•˜์ž.

์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„์˜ ์ž‘์—… ์Šค์ผ€์ค„๋Ÿฌ crontab์„ ํŽธ์ง‘ ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•œ๋‹ค. ๋งŒ์ผ ์–ด๋–ค ํŽธ์ง‘๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋ƒ๊ณ  ๋ฌผ์–ด์˜ค๋ฉด nano ํŽธ์ง‘๊ธฐ๋ฅผ ๋œปํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

sudo crontab -e

์›ํ•˜๋Š” ์‹œ๊ฐ„์„ ์ฃผ๊ธฐ๋กœ ddns ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•˜์ž.

*/5 * * * * /usr/local/etc/ddns-cloudflare.sh

*/5 * * * * ๋ถ€๋ถ„์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ๋ถ„, ์‹œ, ์ผ, ์›”, ์š”์ผ์„ ์˜๋ฏธํ•œ๋‹ค.

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด 5๋ถ„๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

๋ณธ์ธ์ด ์›ํ•˜๋Š” ์Šค์ผ€์ค„์„ crontab์˜ ๋๋ถ€๋ถ„์— ๊ธฐ๋กํ•˜๊ณ , Ctrl ํ‚ค์™€ x ํ‚ค๋ฅผ ๋™์‹œ์— ๋ˆŒ๋Ÿฌ nano ํŽธ์ง‘๊ธฐ๋ฅผ ๋น ์ ธ๋‚˜์˜ค๋ฉด์„œ ์ €์žฅํ•œ๋‹ค.

sudo crontab -l ๋ช…๋ น์œผ๋กœ crontab์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


๋งˆ์น˜๋ฉฐ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” DDNS๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณผ์ •์„ ๋‹ค๋ค˜๋‹ค.

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” Let's Encrypt ๋ฌด๋ฃŒ ์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ๋ฐ ์ž๋™๊ฐฑ์‹  ์„ค์ • ๊ณผ์ •์„ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ค.

profile
๊ฐœ๋ฐœ์ด ์žฌ๋ฐŒ์–ด์„œ ํ•˜๋Š” Junior Backend Developer
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 8์›” 30์ผ

์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ˆ์ „๋ถ€ํ„ฐ ์ž˜ ๋ณด๊ณ  ๋”ฐ๋ผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

/usr/local/etc/ddns-cloudflare.sh: line 31: /var/tmp/ip.txt: Permission denied
mydomain.com myip success:true,errors:[],messages:[]
code: 1001, error: method_not_allowed

31๋ฒˆ Line Prermission denied ๋ฉ”์‹œ์ง€๊ฐ€ ์ฐํžˆ๋Š”๋ฐ.... ์ด๊ฑฐ ์™œ ์ด๋Ÿฐ๊ฑธ๊นŒ์š”???

๋ณด๊ณ  ๋”ฐ๋ผ๋งŒ ํ•  ์ค„ ์•„๋Š” ์ดˆ๋ณด์ž…๋‹ˆ๋‹ค.^^ ๊พธ๋ฒ…

์ด๋ฒˆ์— ubunto 22.04 ์„œ๋ฒ„ ํด๋ฆฐ ์„ค์น˜ ํ›„ ๋‹ค์‹œ ์„ธํŒ…ํ•˜๊ณ  ์žˆ์–ด์š”..ใ…œใ…œ

ํด๋ผ์šฐ๋“œํ”Œ๋ ˆ์–ด์— ๊ฐ€์„œ ํ™•์ธํ•˜๋‹ˆ IP๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜๋Š”๋ฐ...์„ค์ •์— porxy๋กœ ๋ ˆ์ฝ”๋“œ ์†์„ฑ์ด ๋ณ€๊ฒฝ์ด ๋˜๋„ค์š”...ใ…œใ…œ

ํ•ด๋‹น ํด๋”์— ์‚ฌ์šฉ์ž ๊ถŒํ•œ์ด ์—†์–ด์„œ ๊ทธ๋žฌ์–ด์š”....ใ…œใ…œ

๊ฒ€์ƒ‰ํ•ด์„œ ์ž˜ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

1๊ฐœ์˜ ๋‹ต๊ธ€