terminal.cab

Linux Basics Lesson 32 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Who listens, and what answers

9 minutes

Learning goals

ss: who listens where

ss shows the sockets of the machine. Four letters make up the line you will type most: t for TCP, l for listening, n for numbers instead of names, p for the process:

ss -tlnp

Purpose: Shows which program listens on which address and port.

Syntax: ss -tlnp | ss -tuln

Options:

  • -t, -l TCP, and only sockets that listen (-u adds UDP)
  • -n numbers instead of names, 22 instead of ssh
  • -p the process, for other users' processes only with sudo

Example:

ss -tlnp
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN  0        4096             0.0.0.0:22            0.0.0.0:*               
LISTEN  0        4096             0.0.0.0:80            0.0.0.0:*               
LISTEN  0        4096           127.0.0.1:8080          0.0.0.0:*     users:(("python3",pid=2114,fd=3))

in the glossary: ss

Three programs wait for connections. The column Local Address:Port is the one to read. 0.0.0.0:22 means port 22 on every address of the machine, so SSH can be reached from outside. 127.0.0.1:8080 means port 8080 on the loopback only.

The column Process shows a name only for your own processes. python3 app.py belongs to lerner, so you see it. sshd and nginx belong to root, and their column stays empty. With sudo you see all of them:

sudo ss -tlnp
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN  0        4096             0.0.0.0:22            0.0.0.0:*     users:(("sshd",pid=612,fd=3))
LISTEN  0        4096             0.0.0.0:80            0.0.0.0:*     users:(("nginx",pid=701,fd=3))
LISTEN  0        4096           127.0.0.1:8080          0.0.0.0:*     users:(("python3",pid=2114,fd=3))

On this training machine sudo asks for no password. On a real server it asks for yours.

ss -tuln adds UDP and leaves out the processes, a quick overview of everything that is open:

ss -tuln
Netid   State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   
tcp     LISTEN  0        4096             0.0.0.0:22             0.0.0.0:*      
tcp     LISTEN  0        4096             0.0.0.0:80             0.0.0.0:*      
tcp     LISTEN  0        4096           127.0.0.1:8080           0.0.0.0:*      

In older guides you will find netstat -tlnp. It belongs to net-tools, like ifconfig, and new Debian, Ubuntu and RHEL systems no longer install it. ss takes the same letters. On a real server you would often narrow the list down with ss -tlnp | grep 8080. grep, cat and kill are not loaded on this page and answer with command not found, and the list here is short enough to read.

Terminal task 1/3

Mission

Find out with ss which process listens on port 8080, and on which address.

Task 1/3: Find out with ss which process listens on port 8080, and on which address.

Mission

Find out with ss which process listens on port 8080, and on which address.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

A port that listens is not yet a port that can be reached. A firewall can still stand in between, ufw on Debian and Ubuntu, firewalld on RHEL and Rocky Linux. That comes later in the course. For now, ss tells you what the machine itself offers.

curl: what answers

curl fetches a web address from the shell, without a browser, and prints the body of the answer. Without a scheme in front of the address, curl uses HTTP, and -s hides the progress meter:

curl -s shop.example.net/

Purpose: Fetches a web address from the shell and shows the answer.

Syntax: curl [-I] [-s] [-o FILE] URL

Options:

  • -I only the status line and the headers
  • -s silent, no progress meter
  • -o FILE writes the body into FILE instead of the screen

Example:

curl -s shop.example.net/
<h1>shop</h1>

in the glossary: curl

The page came back, so the server answers. How it answered is in the status code. curl -I shop.example.net/ shows only the status line and the headers, and the status line reads HTTP/1.1 200 OK. Try it in the terminal. 2xx means it worked, 3xx sends you elsewhere, 4xx means the request was wrong, for example 404 for a page that does not exist, and 5xx means the server has a problem.

Scripts usually want the number alone. -o writes the body into a file, and -w prints only what you ask for, here the status code:

curl -s -o page.html -w '%{http_code}\n' shop.example.net/missing
404

404: the server is there, but the page is not. That is something different from a server that does not answer at all.

Practice

Terminal task 2/3

The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.

Task 2/3: The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.

The process column is empty for port 22 and port 80. Show the listening TCP sockets with the processes of every user.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 3/3

Does shop.example.net/missing exist? Find the status code of the answer.

Task 3/3: Does shop.example.net/missing exist? Find the status code of the answer.

Does shop.example.net/missing exist? Find the status code of the answer.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Fill the gap

ss shows 0.0.0.0:22. That means: port 22 on addresses of the machine.

ss shows 0.0.0.0:22. That means: port 22 on … addresses of the machine.

Check works once you have typed an answer.

Predict the output

Command

curl -s -o /dev/null -w '%{http_code}\n' shop.example.net/

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does curl -s -o /dev/null -w '%{http_code}\n' shop.example.net/ print?

Check works once you have chosen an answer.

Next up: names, and the other machines.

Quiz

  1. Question 1 of 3As lerner, ss -tlnp shows 0.0.0.0:22, but the column Process is empty. Why?

    Check works once you have chosen an answer.

  2. Question 2 of 3Which status code says that the server found nothing under this address?

    Check works once you have chosen an answer.

  3. Question 3 of 3An old guide says netstat -tlnp. What do you type on a new Debian?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. Card 3 of 3

    Tap the card or press Space to turn it over.

End of the lesson

Still open: 5 exercises, 3 quiz questions

Next: lesson 33, Names and other machines (8 min)

What you can do now: ss -tlnp, curl -s shop.example.net/

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: done
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 33, Names and other machines (8 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.