الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

curl

Send requests to URLs (HTTP, HTTPS, FTP …): test web servers, call APIs, download files, check TLS.

المستوى
لينكس اليومي
المجموعة
الويب
يحتاج root
لا
الحزمة
curl

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

curl URL prints the answer of a server. With options you choose the method (-X), send headers (-H) and data (-d), follow redirects (-L), save to a file (-o), and see headers (-I, -i) or the full talk with the server (-v).

It is the tool to test a web server on the RHCSA exam (curl http://localhost) and the daily tool for REST APIs in DevOps.

curl has more than 250 options. This page lists the ones admins use most; see all with curl --help all.

طريقة الكتابة

curl [OPTION]... URL...

الخيارات 57

الأكثر استخدامًا

Request

-X, --request METHOD
The HTTP method: GET, POST, PUT, DELETE, PATCH …
-H, --header "NAME: VALUE"
Add a header.
-d, --data DATA
Send data with POST (@file reads a file).
--data-raw DATA
Send data; @ has no special meaning.
--data-binary DATA
Send data exactly as it is.
--data-urlencode DATA
URL-encode the data.
--json DATA
Send JSON (sets Content-Type and Accept).
-F, --form NAME=CONTENT
Send a form (multipart), file=@path uploads a file.
-G, --get
Put -d data in the URL as a query string.
-T, --upload-file FILE
Upload a file (PUT).
-A, --user-agent TEXT
Set the User-Agent.
-e, --referer URL
Set the Referer.
-u, --user USER[:PASSWORD]
Basic login (asks the password if not given).
--oauth2-bearer TOKEN
Send a bearer token.

Output

-o, --output FILE
Save to this file.
-O, --remote-name
Save with the name from the URL.
-J, --remote-header-name
Use the file name the server sends (with -O).
-i, --include
Show the response headers with the body.
-I, --head
Get only the headers (HEAD request).
-D, --dump-header FILE
Save the headers to a file.
-s, --silent
No progress bar and no errors.
-S, --show-error
Show errors even with -s.
-v, --verbose
Show the whole request and response, also TLS.
-w, --write-out FORMAT
Print data after the transfer, like %{http_code}.
--trace FILE
Full trace with data.
-#, --progress-bar
Simple progress bar.
--create-dirs
Make local folders for -o.

Connection

-L, --location
Follow redirects.
--max-redirs N
Maximum redirects.
-m, --max-time SECONDS
Stop after this many seconds.
--connect-timeout SECONDS
Maximum time to connect.
--retry N
Try again N times on errors.
-x, --proxy [PROTOCOL://]HOST[:PORT]
Use a proxy.
--noproxy LIST
Hosts that do not use the proxy.
--resolve HOST:PORT:ADDRESS
Use this IP for the host (test before a DNS change).
--connect-to HOST1:PORT1:HOST2:PORT2
Connect to another host/port.
-4, --ipv4
Use IPv4 only.
-6, --ipv6
Use IPv6 only.
--interface NAME
Use this interface.
-C, --continue-at OFFSET
Continue a download (-C - = automatic).
--limit-rate SPEED
Limit the speed, like 1M.
-Z, --parallel
Do transfers in parallel.

TLS

-k, --insecure
Do not check the certificate (tests only).
--cacert FILE
Trust this CA file.
--cert FILE[:PASSWORD]
Client certificate.
--key FILE
Private key of the client certificate.
--tlsv1.2, --tlsv1.3
Use at least this TLS version.
--http1.1, --http2, --http3
Use this HTTP version.

Errors and others

-f, --fail
Exit with an error (22) on HTTP errors, no body.
--fail-with-body
Like -f but print the body.
-n, --netrc
Read login data from ~/.netrc.
-K, --config FILE
Read options from a file.
-q, --disable
Do not read ~/.curlrc (must be first).
-h, --help [CATEGORY]
Show help (--help all for every option).
-V, --version
Show the version and protocols.

أمثلة 12

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Test a web server

    curl http://localhost

    RHCSA: check that httpd serves the page.

  2. 2Only the headers

    curl -I https://s0sta.com
    HTTP/2 200
    content-type: text/html; charset=UTF-8
    strict-transport-security: max-age=31536000

    Check status code and security headers.

  3. 3Only the status code

    curl -s -o /dev/null -w '%{http_code}\n' https://s0sta.com
    200

    Good for health checks and scripts.

  4. 4Follow redirects

    curl -sIL http://s0sta.com | grep -iE '^(HTTP|location)'

    Shows the chain http → https.

  5. 5Download a file

    curl -LO https://example.com/tool.tar.gz

    Saves as tool.tar.gz. Check the checksum afterwards.

  6. 6Call a JSON API

    curl -s -H "Authorization: Bearer $TOKEN" https://api.example.com/v1/servers | jq '.[].name'

    Keep tokens in variables or files, not in the history.

  7. 7Send JSON with POST

    curl -s -X POST -H 'Content-Type: application/json' -d '{"name":"web1"}' https://api.example.com/v1/servers

    Or shorter: --json '{"name":"web1"}'.

  8. 8Test a site on a new server before the DNS change

    curl -sI --resolve s0sta.com:443:203.0.113.20 https://s0sta.com

    Uses the new IP with the real name and certificate.

  9. 9See the TLS handshake and certificate

    curl -sv https://s0sta.com -o /dev/null 2>&1 | grep -E 'SSL connection|subject:|expire date'

    Check TLS version and expiry.

  10. 10Measure response times

    curl -s -o /dev/null -w 'dns %{time_namelookup}s connect %{time_connect}s total %{time_total}s\n' https://s0sta.com

    Find out if DNS, the network or the app is slow.

  11. 11Fail in a script on HTTP errors

    curl -fsS https://example.com/health || echo "health check failed"

    -f makes 404/500 an error (exit code 22).

  12. 12Check from inside a container network

    podman run --rm registry.access.redhat.com/ubi10/ubi curl -sI http://web:8080

    Tests service-to-service access.

انتبه

  • -k turns off certificate checks: never in production scripts.
  • curl URL | bash runs code from the internet without a check. Download, read, then run.
  • Passwords with -u user:pass show up in the process list and history.

الملفات

~/.curlrc
Default options.
~/.netrc
Login data for -n.

رموز الخروج

0 :: Done. 6 :: Could not resolve the host. 7 :: Could not connect. 22 :: HTTP error with -f. 28 :: Timeout. 35 :: TLS handshake failed. 60 :: The certificate could not be checked.