Skip to content

Instantly share code, notes, and snippets.

@cmccormack
Last active January 23, 2023 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmccormack/6e39d2d132dd012bbdc8503c59392ad8 to your computer and use it in GitHub Desktop.
Save cmccormack/6e39d2d132dd012bbdc8503c59392ad8 to your computer and use it in GitHub Desktop.
Linux Networking Commands
# View device IP Address(es)
ip -o address show [interface] | awk '{print $4}' # Prints the IP address(es) of a single interface
ip address # Displays all interfaces
ifconfig # (deprecated)
hostname -I # Displays only IP addresses
# DNS name and resolution
host
dig
/etc/hosts # Add manual DNS record to hostname mappings (normally checked before configured DNS server(s))
/etc/nsswitch.conf # Can be used to change the order of lookups
# View device hostname
hostname
hostname -f
uname -n
# View routing table ( -n skips DNS resolution )
# Using route
route -n
# Using ip
ip route show
# Add/Delete static routes
# Using route
sudo route add -net 192.168.2.0/24 dev eth3
sudo route del -net 192.168.2.0/24 dev eth3
# Using ip
sudo ip route add 192.168.2.0/24 dev eth3
sudo ip route del 192.168.2.0/24 dev eth3
# Add/Delete Default Gateway
# Using route
sudo route add default gw 192.168.0.1
sudo route del default gw 192.168.0.1
# Using ip
sudo ip route add default via 192.168.0.1
sudo ip route del default via 192.168.0.1
# Configure or remove interface with IP Address, Subnet Mask, and Broadcast Address
# Using ifconfig
sudo ifconfig eth0 192.168.0.100 netmask 255.255.255.0 broadcast 192.168.0.255
# Using ip
sudo ip addr add 192.168.0.100/24 broadcast 192.168.0.255 dev eth0
sudo ip addr del 192.168.0.100/24 dev eth0
# Enable/Disable interface
# Using ifconfig
sudo ifconfig eth0 up
sudo ifconfig eth0 down
# Using ifup/ifdown
sudo ifup eth0
sudo ifdown eth0
# Using ip
sudo ip link set eth0 up
sudo ip link set eth0 down
# Modify Maximum Transfer Unit (MTU) on an interface (default: 1500 bytes)
sudo ifconfig eth0 mtu 1496
# Add static interface configuration in /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.0.100/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
# Add static interface configurations in /etc/network/interfaces
iface eth0 inet static
address 10.0.0.41
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
gateway 10.0.0.1
dns-nameservers 10.0.0.1 8.8.8.8
dns-domain acme.com
dns-search acme.com
# Validate /etc/network/interfaces configuration
ifquery --list # Displays available interfaces
ifquery [interface]
# Restart networking service
sudo /etc/init.d/networking restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment