It is a skill, for sure.
I still feel like I suck at it in many ways. But recognizing it is the first step to doing better. And pausing what you're doing when someone talks to you is a relatively easy thing to do.
The "epidemic of loneliness" (in the western world) is a made up thing by the media. There is no actual data to support it, there is some data that shows the opposite.
There is some data that suggest very old people are more lonely but that might just be became on average they are much longer alive and independent after their partner dies compare to past generations.
Here is a short ~5min video about the topic https://youtu.be/rVsKWQp-sq4
Giving and receiving attention is also a skill. You can get better at it.