Basic Linux Network Commands You Must Know

Summary: It's not every day we talk about the "command line side" of Linux at It's FOSS. Basically, I'm more focused on the desktop side of Linux. But some of you readers pointed out in an internal survey (for It's FOSS newsletter subscribers only) that you also want to learn some command-line tricks.

It's not every day we talk about the "command line side" of Linux at It's FOSS. Basically, I'm more focused on the desktop side of Linux. But some of you readers pointed out in an internal survey (for It's FOSS newsletter subscribers only) that you also want to learn some command-line tricks. The cheat sheet is also liked and supported by most readers.

To this end, I compiled a list of basic networking commands in Linux. It is not a tutorial to teach you how to use these commands, but a collection of commands and their short explanations. So, if you've used these commands, you can use it to quickly remember the commands.

You can bookmark this page for quick reference, or export a PDF version for offline use.

I had this list of Linux networking commands when I was a communications systems engineering student. It helped me get high marks in computer networking courses. Hope it helps you in the same way.

List of Linux Basic Networking Commands
I use FreeBSD in my computer networking course, but these UNIX commands should work equally well on Linux.

Connectivity
ping <host>: Send an ICMP echo message (one packet) to the host. This will probably keep sending until you hit Control-C. Pinging means that a packet is sent from your machine via ICMP and echoed back at the IP layer. Ping tells you if another host is running.
telnet <host> [port]: communicate with the host on the specified port. The default telnet port is 23. Press Control-] to exit telnet. Some other commonly used ports are:
7 - echo port
25 - SMTP, for sending mail
79 - Finger Information about other users under the network.
ARP
ARP is used to translate IP addresses to Ethernet addresses. The root user can add and delete ARP records. Deleting ARP records can be useful when they are polluted or wrong. ARP records explicitly added by root are permanent - so are those set by the proxy. The ARP table is kept in the kernel and is manipulated dynamically. ARP records are cached and typically expire and be deleted after 20 minutes.

arp -a: Print the ARP table.
arp -s <ip_address> <mac_address> [pub]: Add a record to the table.
arp -a -d: Delete all records in the ARP table.
Routing
netstat -r: print routing table. Routing tables are kept in the kernel and are used by the IP layer to route packets to non-local networks.
route add: The route command is used to add static (manually specified rather than dynamic) routing paths to the routing table. All traffic from this PC to that IP/subnet will go through the specified gateway IP. It can also be used to set a default route. For example, using 0.0.0.0 at the IP/subnet will send all packets to a specific gateway.
routed: The BSD daemon that controls dynamic routing. Start at boot. It runs the RIP routing protocol. Only the root user is available. You cannot run it without root privileges.
gated: gated is another routing daemon that uses the RIP protocol. It supports OSPF, EGP and RIP protocols simultaneously. Only the root user is available.
traceroute: Used to trace the route of IP packets. It increments the hop count by 1 each time a packet is sent, so that all gateways from the source address to the destination return the message.
netstat -rnf inet: Displays the routing table for IPv4.
sysctl net.inet.ip.forwarding=1: Enable packet forwarding (turn the host into a router).
route add|delete [-net|-host] <destination> <gateway>: (eg route add 192.168.20.0/24 192.168.30.4) to add a route.
route flush: delete all routes.
route add -net 0.0.0.0 192.168.10.2: Add a default route.
routed -Pripv2 -Pno_rdisc -d [-s|-q]: Run the routed daemon, use the RIPv2 protocol, do not enable ICMP autodiscovery, run in the foreground, feed mode or quiet mode.
route add 224.0.0.0/4 127.0.0.1: Defines a multicast route for local addresses. (LCTT Annotation: The original text is in doubt)
rtquery -n <host> (LCTT Annotation: The host parameter is added): Query the RIP daemon on the specified host (manually update the routing table).
other
nslookup: Query to DNS server to convert IP to name, or vice versa. For example, nslookup facebook.com will give the IP of facebook.com.
ftp <host> [port] (LCTT Annotation: water in the original text should be a typo): Transfer files to the specified host. You can usually log in using the login name "anonymous" and the password "guest".
rlogin -l <host> (LCTT Annotation: added host parameter): Use a virtual terminal like telnet to log in to the host.
Important files
/etc/hosts: Mapping of domain names to IP addresses.
/etc/networks: A mapping of network names to IP addresses.
/etc/protocols: A mapping of protocol names to protocol numbers.
/etc/services: A mapping of TCP/UDP service names to port numbers.
Tools and Network Profiling
ifconfig <interface> <address> [up]: Bring up the interface.
ifconfig <interface> [down|delete]: Stop the interface.
ethereal &: Open ethereal in the background instead of the foreground.
tcpdump -i -vvv: Tool for scraping and analyzing packets.
netstat -w [seconds] -I [interface]: Displays network settings and statistics.
udpmt -p [port] -s [bytes] target_host: Send UDP traffic.
udptarget -p [port]: Receive UDP traffic.
tcpmt -p [port] -s [bytes] target_host: Send TCP traffic.
tcptarget -p [port]: Receive TCP traffic.
switch
ifconfig sl0 srcIP dstIP: configure a serial interface (execute slattach -l /dev/ttyd0 before, then sysctl net.inet.ip.forwarding=1)
telnet 192.168.0.254: from a host in the subnet Access the switch.
sh ru or show running-configuration: View the current configuration.
configure terminal: Enter the configuration mode.
exit: Exit the current mode. (LCTT Annotation: The original text is in doubt)
VLAN
vlan n: Create a VLAN with ID n.
no vlan N: delete the VLAN with ID n.
untagged Y: Add port Y to VLAN n.
ifconfig vlan0 create: Create the vlan0 interface.
ifconfig vlan0 vlan_ID vlandev em0: Add em0 to the vlan0 interface (LCTT Annotation: Original questionable), and set the tag as ID.
ifconfig vlan0 [up]: Enable virtual interface.
tagged Y: Add tagged frame support for port Y of the current VLAN.
UDP/TCP
socklab udp: Run socklab using the UDP protocol.
sock: Creates a UDP socket, equivalent to typing sock udp and bind.
sendto <Socket ID> <hostname> <port #>: Send packets.
recvfrom <Socket ID> <byte #>: Receive data from socket.
socklab tcp: Run socklab using the TCP protocol.
passive: Create a passive mode socket, equivalent to socklab, sock tcp, bind, listen.
accept: accepts incoming connections (can be executed before or after initiating incoming connections).
connect <hostname> <port #>: Equivalent to socklab, sock tcp, bind, connect.
close: Close the connection.
read <byte #>: Read n bytes from the socket.
write: (eg, write ciao, write #10) Writes "ciao" or 10 bytes to the socket.
NAT/Firewall
rm /etc/resolv.conf: Disable address resolution to ensure your filtering and firewall rules work correctly.
ipnat -f file_name: Write filter rules to a file.
ipnat -l: Display a list of active rules.
ipnat -C -F: Reinitialize the rule table.
map em0 192.168.1.0/24 -> 195.221.227.57/32 em0: Map IP addresses to interfaces.
map em0 192.168.1.0/24 -> 195.221.227.57/32 portmap tcp/udp 20000:50000: map with port number.
ipf -f file_name: Write filter rules to a file.
ipf -F -a: reset the rule table.
ipfstat -I: List active status entries when used with the -s option.
The original release time is: 2016-09-23

This article is from Yunqi Community Partner "Linux China"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326196069&siteId=291194637