curl 명령어
curl은 CLI 환경에서 HTTP/HTTPS 요청을 전송하고 응답을 확인할 수 있는 가장 기본적이면서도 강력한 도구입니다. API 테스트, 파일 다운로드, 프록시/네트워크 디버깅, 웹 성능 측정 등 다양한 상황에서 활용할 수 있습니다.
1. 기본 구문
curl [options...] <URL>주요 옵션
| 옵션 | 설명 |
|---|---|
| -X | HTTP 메서드 지정 (GET, POST 등) |
| -d | POST 데이터 전송 |
| -H | HTTP 헤더 설정 |
| -O | 원본 파일명으로 다운로드 |
| -s | Silent 모드 (출력 최소화) |
| -S | 에러 발생 시 메시지 출력 |
| -f | HTTP 에러 코드(4xx/5xx) 시 실패 처리 |
| -w | 요청 완료 후 추가 정보 출력 |
| –noproxy | 프록시를 사용하지 않을 호스트 목록 지정 |
| -x, –proxy | 지정한 프록시 서버 사용 |
| –resolve | DNS 조회를 무시하고 특정 IP로 강제 연결 |
2. 기본 사용 예시
GET 요청 보내기
curl http://blog.scbyun.comPOST 요청 보내기
curl -X POST -d "param1=value1¶m2=value2" http://blog.scbyun.com파일 다운로드
curl -O http://blog.scbyun.com/file.zipHTTP 헤더 설정
curl -H "Content-Type: application/json" http://blog.scbyun.com/api인증서를 사용한 HTTPS 요청
curl --cert /tmp/cert.pem --key /tmp/key.pem https://blog.scbyun.com3. 프록시 사용
프록시를 사용하지 않도록 설정(–noproxy)
curl --noproxy example.com http://blog.scbyun.comcurl --noproxy localhost,127.0.0.1 http://blog.scbyun.com- 특정 호스트만 프록시를 우회해야 할 때 유용합니다.
프록시 서버 사용(–proxy, -x)
curl --proxy http://proxy-server:8080 http://blog.scbyun.comcurl -x proxy.example.com:8080 -U username:password http://blog.scbyun.com4. DNS 강제 지정(–resolve)
DNS를 변경하지 않고 특정 IP로 요청을 보내야 할 때 사용합니다.
curl --resolve example.com:80:203.0.113.1 http://blog.scbyun.com- 테스트 서버 점검, 로드밸런서 우회 테스트 시 매우 유용합니다.
5. 스크립트 다운로드 활용
스크립트만 다운로드
curl -Ssf https://blog.scbyun.com/scripte/webconfsync.sh -o /tmp/webconfsync.sh스크립트 다운로드 후 즉시 실행
curl -Ssf https://blog.scbyun.com/scripte/webconfsync.sh | bash -x- -Ssf 옵션 조합은 에러 발생 시 즉시 실패하도록 만들어 자동화에 적합합니다.
wget 을 사용한 동일한 방식
wget -qO - https://blog.scbyun.com/scripte/webconfsync.sh | bash -x6. HTTP 상태 코드 확인
curl -s -o /dev/null -w '%{http_code}\n' https://blog.scbyun.com2007. 웹사이트 응답 속도 측정
간단한 응답 시간 측정
curl -s -w '\nTesting Website Response Time for: %{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://blog.scbyun.comTesting Website Response Time for: https://blog.scbyun.com/
Lookup Time: 0.004
Connect Time: 0.007
Pre-transfer Time: 0.208
Start-transfer Time: 0.526
Total Time: 0.529포맷 파일을 이용한 상세 측정
curl-format.txt 생성
cat <<EOF > curl-format.txt
time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_redirect: %{time_redirect}
time_starttransfer: %{time_starttransfer}
----------
time_total: %{time_total}
EOF포맷 파일 적용
curl -w "@curl-format.txt" -o /dev/null -s https://blog.scbyun.comtime_namelookup: 0.004
time_connect: 0.007
time_appconnect: 0.210
time_pretransfer: 0.210
time_redirect: 0.000
time_starttransfer: 0.488
----------
time_total: 0.488특정 IP로 HTTPS 성능 테스트
curl -w "@curl-format.txt" -o /dev/null -s \
--resolve blog.scbyun.com:443:111.111.111.111 \
https://blog.scbyun.com9. 반복 측정으로 평균 체감 확인
for i in {1..3}; do
curl -s -w "%{time_total}\n" -o /dev/null https://blog.scbyun.com
done0.689
0.566
0.737참고URL