All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
A person who passes by a grave should greet them with Salaam as proven in the Sunnah.
Sulaymaan ibn Buraydah narrated from his father that he said: “The Prophet used to teach them when they went out to the graveyard. One of them would say: As-Salaamu ‘alaykum ahlad-diyaar minal-mu’mineena wal-muslimeena, wa inna inshaa’ Allahu la laahiqoon, as’alul-laaha lana wa lakumul-‘aafiyyah (Peace be upon you, O inhabitants of the abodes, believers and Muslims, and we will join you soon if Allah wills. We ask Allah for well-being for us and for you).” [Muslim]
Besides, ‘Aa’ishah asked: “What should I say, O Messenger of Allah?” He said: ‘Say” Peace be upon the inhabitants of this place among the believers and Muslims. May Allah have mercy upon those who have gone on ahead of us and those who will come later on, and we will join you, if Allah wills”’ [Muslim]
Shaykh Ibn ' Uthaymeen was asked in Fataawa Noorun ‘ala ad-Darb:
“Dear Shaykh, if I pass through a walled cemetery, shall I greet them with Salaam or do I have to enter the cemetery?
“I cannot issue a decisive answer to this; sometimes I say one should greet them with Salaam because the cemetery is a home for those who are dead and sometimes I say one does not greet them with Salaam. If a person passes by a man's house knowing that the man is in the same house, he does not greet him with Salaam until he meets him or enters upon him or stands at his door seeking the permission (to enter); so I cannot issue a decisive answer to this. But if he greets them with Salaam, then I hope that there is no harm in this.” [End of quote]
For more benefit, please refer to Fatwa 306583.
Allah knows best.