logo

Linux - Networking

Last Updated: 2023-02-14

There are top-like tool for network, but need to be installed separately, e.g. iftop

Check IP Routes:

$ ip route

Display TCP/IP and other packets being transmitted or received over a network:

$ sudo tcpdump

Ping, sending ICMP echo request:

$ ping wikipedia.org

Trace the route

$ traceroute wikipedia.org

Query Name Servers:

$ nslookup wikipedia.org

Network Interface Names

Naming conventions (what you may see in ip a):

  • if it has firmware or BIOS-provided index numbers for onboard devices. => eno1
  • else if it has firmware or BIOS-provided PCI Express (PCIe) hot plug slot index numbers => ens1
  • else if it has the physical location of the connector of the hardware => enp2s0
  • else => eth0 (The traditional unpredictable kernel naming scheme.)

to config:

/usr/lib/systemd/network/99-default.link => NamePolicy

nmcli

For controlling NetworkManager.

Show connections

$ nmcli connection show

OpenSSL

$ openssl rand -base64 10
129udXpYaQJZeg==

curl

Retrieve a page

$ curl www.example.com

Save to a file

$ curl -o example.html www.example.com

Save as the origin name

$ curl -O www.example.com/example.html

Redo the request if page was moved(3XX response code)

$ curl -OL www.example.com/example.html

Get

$ curl -X GET www.example.com

Put

$ curl -X PUT --data-binary @file.xml -H "Content-type: text/xml" http://example.com/put

Use -d(--data) or --data-binary to send data.

-d sends the Content-Type application/x-www-form-urlencoded, to specify another content type, use -H "Content-Type: application/json", e.g.

$ curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

dig

dig (domain information groper): DNS lookup utility

Unless it is told to query a specific name server, dig will try each of the servers listed in /etc/resolv.conf

Example

$ dig google.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13686
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		8	IN	A	xxx.xxx.xxx.xxx

;; Query time: 16 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Thu Jan 01 20:24:55 PDT 2020
;; MSG SIZE  rcvd: 55

DNS

/etc/systemd/resolved.conf

Hostname

# Show hostname
$ hostname
$ cat /etc/hosts

# Show IP
$ hostname -i

# Show all IPs
$ hostname -I

# Set hostname
$ hostname newname
  • /etc/hostname: the hostname of the machine.
  • /etc/hosts: list of hosts.
  • host <host_name>: DNS lookup.

Unknown Host Error in sudo: make sure the names match

$ cat /etc/hosts
127.0.1.1   example-hostname

$ cat /etc/hostname
example-hostname

$ host example-hostname
example-hostname.foo.bar.example.com has address 10.64.xxx.xxx