I was riding behind the Messenger of Allah ﷺ one day when he said, "Young man, let me teach you some words. Be mindful of Allah, and He will protect you. Be mindful of Allah, and you will find Him before you. When you ask, ask Allah. When you seek help, seek help from Allah. Know that if the entire nation were to gather together to benefit you with something, they could not benefit you except with what Allah has already written for you, and if they were to gather together to harm you with something, they could not harm you except with what Allah has already written against you. The pens have been lifted and the pages have dried."
كنت خلفت رسول الله ﷺ يوما، فقال: يا غلام إني أعلمك كلمات: احفظ الله يحفظك، احفظ الله تجده تجاهك، إذا سألت فاسأل الله، وإذا استعنت فاستعن بالله، واعلم أن الأمة لو اجتمعت على أن ينفعوك بشيء لم ينفعوك إلا بشيء قد كتبه الله لك، ولو اجتمعوا على أن يضروك بشيء لم يضروك إلا بشيء قد كتبه الله عليك، رفعت الأقلام، وجفت الصحف.
Tirmidhi (2516) related it, and this is his wording, and Ahmad (2669), through routes from Qays bin Hajjaj, from Hanash Sanani, from Ibn Abbas, who mentioned it. Its chain is hasan because of Qays bin Hajjaj, who is Kalai, for his hadith is hasan. Tirmidhi said, "Hasan sahih." The discussion of it is set out at length in the book on faith.