Useful Linux Network commands
by Alex Arica

Set a static IP address

We are going to assign the static IP address 192.168.0.2 with a gateway 192.168.0.1 to a network interface "eth1".

On Ubuntu

Create this file:

sudo vi /etc/netplan/60-static.yaml
                    

Add the followings:

network:
  version: 2
  ethernets:
    eth1:
       dhcp4: no
      addresses:
      - 192.168.0.2/16
      gateway4: 192.168.0.1
                    

Save the file and apply the changes:

sudo netplan apply
                    

On Debian

Open this file:

sudo vi /etc/network/interfaces
                    

Add the followings:

auto eth1
iface eth1 inet static
address 192.168.0.2
netmask 255.255.0.0
gateway 192.168.0.1
                    

Save the file and apply the changes:

sudo systemctl restart networking
                    

Curl

Make a request and display the headers sent and the response received:

curl -livk 137.74.127.96
                    

Allowed private addresses

The following are the reserved IP ranges which are considered as private IPs:

10.0.0.0        -   10.255.255.255  (10/8 prefix)
172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
                    

Mode details about rfc1918

Network IP and interfaces

See all interfaces and their IP addresses:

ip a
ip adr
ip address
                    

Show the gateway from which a server access to internet:

ip route show
                    

Add a default gateway:

ip route add default via 192.168.0.1 dev eth1
                    

Delete ip address from interface:

sudo ip address del 192.168.0.1/16 dev eth1
                    

Restart networking:

sudo systemctl restart networking
                    

Bring up interface:

sudo ip link set eth1 up
                    

Bring down interface:

sudo ip link set eth1 down
                    

Scan ports

Scan 1st 1000 ports of a remote host:

# if not installed run: apt install nmap
sudo nmap -Pn [ip address to scan]
                    

Scan port 22:

sudo nmap -Pn -p 22 [ip address to scan]
                    

Scan a range of ports between 0 and 65535:

sudo nmap -Pn -p0-65535 [ip address to scan]
                    

Scan all 65535 ports:

sudo nmap -Pn -p- [ip address to scan]
                    

Run the below from the server and compare the values from nmap:

netstat -tuplen