Imam Aḥmad, al-Tirmidhī, al-Nasāʾī, al-Bukhārī in his Book of Etiquette, and al-Ḥākim in his Mustadrak all transmit—on the authority of al-Ḥajjāj ibn Arṭāh, who said: Ibn Maṭar narrated to me, from Sālim, from his father—that the Messenger of Allah ﷺ, whenever he heard thunder and lightning, would say:
“O Allah, do not kill us with Your wrath, do not destroy us with Your punishment, and grant us well-being before that.”
Ibn Jarīr also relates—through Layth, from a man, from Abū Hurayrah, in a marfūʿ report—that whenever he heard thunder he would say:
“Glory be to the One whom the thunder praises by extolling Him.”
ʿAlī used to say, “Glory be to Him whom you praise.” The same is reported from Ibn ʿAbbās, al-Aswad ibn Yazīd, Ṭāwūs, and others.
Mālik narrates—from ʿĀmir ibn ʿAbd Allāh ibn al-Zubayr—that when he heard thunder he would break off whatever conversation he was engaged in and say:
“Glory be to the One whom the thunder praises by extolling Him, and (so do) the angels out of awe of Him,” and he would add, “Indeed, this is a severe warning to the people of the earth.”
Imam Aḥmad further relates—from Abū Hurayrah—that the Messenger of Allah ﷺ said:
“Your Lord has declared: If My servants were to obey Me, I would send them rain by night and let the sun shine upon them by day, and they would never hear the sound of thunder. [Al-Ṭabarānī also transmits with a raised chain from Ibn ʿAbbās: ‘When you hear the thunder, remember Allah, for no one who remembers Him will be struck.’]”
All of this is set out in detail in the Tafsīr; to Allah belong all praise and grace.