I think the main thing is just to appear friendly (i.e. say "Hi") and talk about something the other person cares about.
Of course, if they're complete strangers, it can be difficult to get the necessary context. I've struggled with this problem since I was a kid and still haven't figured it out.