I do the same, whenever he bites someone who tries to pet him i say "wow, he never does that, do all dogs hate you?" even though i know damn well he does it to everyone
There's secrets to patting dogs. If you just walk up and start patting it's head, it'll probably move away and disengage. You gotta be able to tell at a glance what kind of doggo it is and what kind of pats it'll want. Get good at this and they'll love you over anyone else. You're the hooman that pats good.
I'd share, but knowing how to pat good is just half of it. There's the approach, body language, eye contact, pace of initial pat, etc. There's no shortcuts of going from pat to belly rubs. You just gotta learn the ways.
Actually the secret is letting the dog sniff your hand first. This way you kinda introduce yourself. This makes it far more likely the dog will be ok with pets. How do I know? I am this meme.
This is a myth, no idea where it came from, but dogs will sniff you if they want to come sniff you. Body language is important and it's important to give the dog agency in the greeting. Putting your hand out towards a dog is not good advice for most people as most people don't understand body language, can be threatening to the dog, and it'll probably get someone bit by a nervous dog.
In general, just ask the owner for permission and information, or keep a comfortable distance and leave the dog be. Dogs are still animals, even if they're usually friendly.