عَنْ عَبْدِ اللَّهِ بْنِ عَبَّاسٍ رَضِيَ اللَّهُ عَنْهُمَا قَالَ: كُنْت خَلْفَ رَسُولِ اللَّهِ صلى الله عليه و سلم يَوْمًا، فَقَالَ: يَا غُلَامِ! إنِّي أُعَلِّمُك كَلِمَاتٍ: احْفَظْ اللَّهَ يَحْفَظْك، احْفَظْ اللَّهَ تَجِدْهُ تُجَاهَك، إذَا سَأَلْت فَاسْأَلْ اللَّهَ، وَإِذَا اسْتَعَنْت فَاسْتَعِنْ بِاللَّهِ، وَاعْلَمْ أَنَّ الْأُمَّةَ لَوْ اجْتَمَعَتْ عَلَى أَنْ يَنْفَعُوك بِشَيْءٍ لَمْ يَنْفَعُوك إلَّا بِشَيْءٍ قَدْ كَتَبَهُ اللَّهُ لَك، وَإِنْ اجْتَمَعُوا عَلَى أَنْ يَضُرُّوك بِشَيْءٍ لَمْ يَضُرُّوك إلَّا بِشَيْءٍ قَدْ كَتَبَهُ اللَّهُ عَلَيْك؛ رُفِعَتْ الْأَقْلَامُ، وَجَفَّتْ الصُّحُفُ، وَفِي رِوَايَةِ غَيْرِ التِّرْمِذِيِّ: احْفَظْ اللَّهَ تَجِدْهُ أمامك، تَعَرَّفْ إلَى اللَّهِ فِي الرَّخَاءِ يَعْرِفُك فِي الشِّدَّةِ، وَاعْلَمْ أَنَّ مَا أَخْطَأَك لَمْ يَكُنْ لِيُصِيبَك، وَمَا أَصَابَك لَمْ يَكُنْ لِيُخْطِئَك، وَاعْلَمْ أَنَّ النَّصْرَ مَعَ الصَّبْرِ، وَأَنْ الْفَرَجَ مَعَ الْكَرْبِ، وَأَنَّ مَعَ الْعُسْرِ يُسْرًا
Meaning: I was behind the Prophet ﷺ one day and he said: Young man, I will 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 whole nation gathered to benefit you, they could only benefit you with what Allah had already written for you, and if they gathered to harm you, they could only harm you with what Allah had already written against you. The pens have been lifted and the pages have dried. In another wording: Be mindful of Allah and you will find Him ahead of you. Know Allah in times of ease and He will know you in times of hardship. Know that what missed you was never going to hit you, and what hit you was never going to miss you. Know that victory comes with patience, relief with hardship, and with difficulty comes ease.
At-Tirmidhi: hasan sahih. Al-Albani: sahih. The added wording that an-Nawawi quotes from outside at-Tirmidhi is, Ibn Rajab says, from Abd ibn Humayd with a weak chain, though he gives similar words from Ahmad.
Narrated by Abdullah ibn Abbas · Jami' at-Tirmidhi 2516