كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
5  
The Book of Mosques and Places of Prayer
Sahih Muslim 520 b

Ibrahim b. Yazid al-Tayml reported:

I used to read the Qur'an with my father in the vestibule (before the door of the mosque). When I recited the ayat (verses) concerning prostration, he prostrated himself. I said to him: Father, do you prostrate yourself in the path? He said: I heard Abu Dharr saying: I asked the Messenger of Allah (ﷺ) about the mosque that was first set up on the earth. He said: Masjid Harim. I said: Then which next? He said: The Masjid al-Aqsa. I said: How long is the space of time between the two? He said: Forty years. He (then) further said: The earth is a mosque for you, so wherever you are at the time of prayer, pray there.
حَدَّثَنِي عَلِيُّ بْنُ حُجْرٍ السَّعْدِيُّ، أَخْبَرَنَا عَلِيُّ بْنُ مُسْهِرٍ، حَدَّثَنَا الأَعْمَشُ، عَنْ إِبْرَاهِيمَ بْنِ يَزِيدَ التَّيْمِيِّ، قَالَ كُنْتُ أَقْرَأُ عَلَى أَبِي الْقُرْآنَ فِي السُّدَّةِ فَإِذَا قَرَأْتُ السَّجْدَةَ سَجَدَ فَقُلْتُ لَهُ يَا أَبَتِ أَتَسْجُدُ فِي الطَّرِيقِ قَالَ إِنِّي سَمِعْتُ أَبَا ذَرٍّ يَقُولُ سَأَلْتُ رَسُولَ اللَّهِ صلى الله عليه وسلم عَنْ أَوَّلِ مَسْجِدٍ وُضِعَ فِي الأَرْضِ قَالَ ‏"‏ الْمَسْجِدُ الْحَرَامُ ‏"‏ ‏.‏ قُلْتُ ثُمَّ أَىٌّ قَالَ ‏"‏ الْمَسْجِدُ الأَقْصَى ‏"‏ ‏.‏ قُلْتُ كَمْ بَيْنَهُمَا قَالَ ‏"‏ أَرْبَعُونَ عَامًا ثُمَّ الأَرْضُ لَكَ مَسْجِدٌ فَحَيْثُمَا أَدْرَكَتْكَ الصَّلاَةُ فَصَلِّ ‏"‏ ‏.‏
Reference : Sahih Muslim 520b
In-book reference : Book 5, Hadith 2
USC-MSA web (English) reference : Book 4, Hadith 1057
  (deprecated numbering scheme)