SH

Networking

Bash & Linux · 10 entries

Transfer Data with URLs

syntax
curl [options] URL
example
curl https://api.example.com/users
curl -X POST -H 'Content-Type: application/json' -d '{"name":"Ada"}' https://api.example.com/users
curl -o release.tar.gz -L https://github.com/proj/repo/archive/v2.1.tar.gz

Note -o saves to a file, -O uses the remote filename. -L follows redirects (essential for GitHub downloads). -s silences progress bar. -I fetches only headers. -k skips TLS verification (use only for debugging, never in production).

Download Files

syntax
wget [options] URL
example
wget https://releases.example.com/v3.1/app.deb
wget -c https://dumps.example.com/backup.sql.gz
wget -r -np -l 2 https://docs.example.com/manual/

Note -c resumes a partially downloaded file. -r downloads recursively, -np prevents ascending to the parent directory, -l sets recursion depth. wget is simpler than curl for straightforward downloads and supports resuming by default.

Secure Shell Remote Login

syntax
ssh [options] user@host
example
ssh deploy@10.0.1.50
ssh -i ~/.ssh/prod_key.pem ec2-user@server.example.com
ssh -L 5432:db-host:5432 bastion@jump.example.com

Note -i specifies a private key file. -L sets up local port forwarding (the example tunnels a remote Postgres port to localhost:5432). -p sets a non-standard SSH port. Add -v for verbose debugging of connection issues.

Secure Copy Between Hosts

syntax
scp [options] source destination
example
scp deploy.tar.gz deploy@10.0.1.50:/opt/releases/
scp -r deploy@server:/var/log/app/ ./remote-logs/
scp -P 2222 config.yml user@host:/etc/app/

Note -r copies directories recursively. -P (capital) specifies a non-standard port. For large or repeated transfers, rsync is preferable as it only sends differences.

Efficient File Synchronization

syntax
rsync [options] source destination
example
rsync -avz --progress ./build/ deploy@web:/var/www/app/
rsync -avz --delete --exclude='.git' src/ backup/src/
rsync -avz -e 'ssh -p 2222' data/ user@host:/data/

Note -a is archive mode (preserves permissions, timestamps, symlinks). -v is verbose. -z compresses during transfer. --delete removes files from the destination that no longer exist in the source (be careful). Trailing slash on source matters: dir/ syncs contents, dir syncs the directory itself.

Test Network Connectivity

syntax
ping [options] host
example
ping -c 5 google.com
ping -c 3 192.168.1.1
output
64 bytes from 142.250.80.46: icmp_seq=1 ttl=118 time=11.2 ms
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss

Note -c limits the number of pings (without it, Linux pings forever; macOS defaults to unlimited too). High time values indicate latency; packet loss indicates connectivity problems.

View Network Connections & Ports

syntax
ss [options]
netstat [options]
example
ss -tlnp
ss -s
netstat -tlnp
output
State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port  Process
LISTEN  0       511     0.0.0.0:80          0.0.0.0:*          users:(("nginx",pid=1482,fd=6))

Note ss is the modern replacement for netstat. -t shows TCP, -u shows UDP, -l shows listening sockets, -n shows numeric ports (not names), -p shows the process using it. netstat is deprecated on many systems but still widely used.

Netcat: Network Swiss Army Knife

syntax
nc [options] host port
example
nc -zv server.example.com 443
echo 'PING' | nc -w 2 redis.local 6379
nc -l 9090
output
Connection to server.example.com 443 port [tcp/https] succeeded!

Note -z scans without sending data (port check). -v is verbose. -w sets a timeout in seconds. -l listens on a port (useful for quick debugging). Netcat is invaluable for testing whether a port is open from a particular host.

DNS Lookup

syntax
dig [options] domain [type]
example
dig example.com
dig example.com MX +short
dig @8.8.8.8 example.com A +trace
output
example.com.  300  IN  A  93.184.216.34

Note +short gives concise output. @server queries a specific DNS server. Common types: A (IPv4), AAAA (IPv6), MX (mail), CNAME (alias), TXT, NS. +trace follows the full delegation chain from root servers.

View & Configure Network Interfaces

syntax
ip addr show
ifconfig
example
ip addr show eth0
ip route show
ifconfig en0
output
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500
    inet 192.168.1.42/24 brd 192.168.1.255 scope global eth0

Note ip is the modern Linux tool; ifconfig is legacy but still default on macOS. 'ip addr' shows addresses, 'ip route' shows the routing table, 'ip link' manages interfaces. On macOS, the primary interface is usually en0.