Praise be to Allaah.
We put this question to Shaykh Muhammad ibn Saalih al-‘Uthaymeen, may Allaah preserve him, who answered as follows:
The fuqaha’ said that it is sunnah for both the one who passes by and the one who enters the graveyard to say salaams to the inhabitants of the graves. Based on that, the one who is passing by should stop, say salaams, then continue on his way. And Allaah knows best.