Almost every computer in the world is now connected to a network. The Linux shell makes it easy to communicate with content on the Internet from the command line, and for the user to check on important network status information. In this exercise, we will introduce a few tools that you may find useful.
Interacting with Files Hosted Online
Let’s start simple: how do we access files from the Internet in the command line? There are two commands that allow us to do just that: curl
and wget
. Either command lets us establish a connection with a server. We can also use them to download the file at the destination.
$ curl <URL in quotes>
displays the contents of the file at the URL in terminal.
The option -o
or -output
combined with a desired name
$ curl <URL> --output <desired_filename>
lets us write the contents of the online file to the filename! For example, curl -O "https://www.google.com" --output frontpage.html
will download the HTML file of the Google homepage to a local file called frontpage.html
.
There are more commands if we type curl --help
or wget --help
. As always, you can also use the man
command to read about the commands.
Checking Network Connectivity
How can we check whether two devices connected to the same network can communicate with one another? Simple! We use a command called ping
, short for Packet INternet Groper. The command
ping [options] <target domain or IP>
sends packets to the target host and waits for replies. If the ping command returns with a failure, that means the target is unreachable.
DNS Resolution
What if we wanted to find out the IP address for any given domain name or vice versa? This process is known as DNS lookup or DNS resolution and the host
command lets us do exactly that. The basic syntax for the host
command is: host <domain or IP>
.
For example, the command host google.com
will generate the following output:
google.com has address 142.250.72.238 google.com has IPv6 address 2607:f8b0:4007:817::200e google.com mail is handled by 20 alt1.aspmx.l.google.com. google.com mail is handled by 40 alt3.aspmx.l.google.com. google.com mail is handled by 30 alt2.aspmx.l.google.com. google.com mail is handled by 10 aspmx.l.google.com. google.com mail is handled by 50 alt4.aspmx.l.google.com.
Network Interface Status
The ifconfig
command, or interface configurator, is one of the network inspection commands available on Linux. Running it will display IP addresses, MAC addresses, and some more relevant details.
These commands are really useful if you work with networks specifically! If you’d like to learn more about networks, check out our Introduction to Network Basics lesson.
As always, we recommend using man
or info
for more detail on these commands as we are barely skimming the surface of what these commands can do!
Instructions
Use wget
to download Codecademy’s homepage, www.codecademy.com
.
Use curl
to download the file from the URL, https://static-assets.codecademy.com/Courses/learn-linux/secret-file.txt
.
Use the host
command to look up the IP address for the domain codecademy.com
Use the ping
command to ping one of the IP addresses you found from the last checkpoint.
Note: Make sure to interrupt ping
with CTRL / CMD + C, otherwise it runs indefinitely. Alternatively, we can specify how many packets we wish to send using the -c
option.
Run the ifconfig
command and observe the result.
On the left, you will see all the available network interfaces, including virtual network interfaces. For each interface, you will see:
HWaddr
: MAC address of the ethernet interfaceinet
: IPv4 address assigned to the interfaceinet6
: IPv6 address assigned to the interfaceMTU
: Maximum transmission unit