Passa al contenuto principale

2. Networking

  1. Basic tools:

    ip address
    ip addr
    ip a
    ip addr show lo
    ip route
    ip r
    ping -c 3 8.8.8.8
    dig linuxcommand.org
    dig linuxcommand.org +short
    ping -c 3 linuxcommand.org

    traceroute linuxcommand.org

    tracepath linuxcommand.org

  2. For downloading files we can use wget or curl:

    wget http://linuxcommand.org/index.php
    less index.php
    wget -O index.html 'http://linuxcommand.org/index.php'
    less index.html
    curl http://linuxcommand.org/index.php
    curl http://linuxcommand.org/index.php > index.html
  3. Netcat is a simple tool for network communication.

    Let's use it to listen to the port 12345:

    nc -l 12345

    Open another terminal and connect to the same port like this:

    nc localhost 12345    # on the second terminal

    Now, any line that you type here is sent and displayed to the other terminal:

    Hello network
    The quick brown fox jumped over the internet

    Check the other terminal.

    Interrupt them with Ctrl-c.

    This may not seem very impressive, but instead of localhost we could have used a real server name or IP and connect to it remotely. It may be used to check that the TCP port 12345 on the server is accessible from the client (in case that there is a firewall, for example).

    For checking a UDP port we can add the option -u to both of these commands.

    It can also be used as a simple tool for file transfer:

    nc -l 12345 > file.txt    # on the first terminal
    nc -w 3 localhost 12345 < /etc/passwd    # on the second terminal
    ls file.txt    # on the first terminal
    cat file.txt    # on the first terminal

    As another example, let's combine it with tar to transfer a whole directory:

    mkdir cptest
    cd cptest
    nc -l 12345 | tar xzpf -

    Switch to the second terminal.

    cd testdir
    ls
    tar czpf - . | nc -w 3 localhost 12345

    Switch to the first terminal.

    ls
    cd ..
    rm -rf cptest
Loading asciinema cast...