And you shall write it upon your doorposts. (Deuteronomy 6:9)
Many people have the custom to kiss the mezuzah when passing through a doorway. The Gemara relates a story about the Roman convert to Judaism called Onkelos (d. 120 CE) who was the nephew of the Roman Emperor Titus (Gittin 56b) and wrote one of the Aramaic translations to the Torah.
In the Talmud (Avodah Zarah 11a) Caeser wanted to arrest Onkelos for converting to Judaism. Successive legions who came to arrest Onkelos were inspired by his teachings and also converted. Caeser instructed the next group of troops not to talk to Onkelos but just arrest him.
On leaving his home, Onkelos touched the mezuzah and asked the Roman troops, ‘What is this? They replied, ‘You tell us.’ He explained that normally a king sits inside and his servants guard him from the outside. God however, guards from the outside while His servants sit on the inside (Psalms 121:8). Impressed by Onkelos, those troops also converted.
Rabbi Moshe Isserlis (known as the Rama, d. 1572) writes that this is the source for touching (or kissing) the mezuzah when passing it. He adds that on leaving the house one should say (based on Psalms 121:8) ‘Hashem Yishmor tzeitzi u’vo’ meiatah v’ad olam’ – God should guard my leaving and coming from now and forever (Yoreh Deah 285:2).
Rabbi Shlomo Ganzfried (d. 1886) writes that one should kiss the mezuzah when he leaves and enters the house and similarly begin ‘Hashem Shomri Hashem Tzili al yad yemini …’ – God is my guard, God is my shade on my right hand …’ before concluding with the Rama’s formula (Kitzur Shulchan Aruch 11:24).
Note that the only sources indicate that the tradition is to touch or kiss the mezuzah of the front door on exiting, not every mezuzah of every door every time one goes from room to room.
Nevertheless, this custom helps to remind us that it is not the mezuzah that protects us, but God and that we need to feel His protection even more when we venture out of the comfort of our homes.