Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:24:27

O you who have believed, do not enter houses other than your own houses until you ascertain welcome and greet their inhabitants. That is best for you; perhaps you will be reminded.  

O you who believe, do not enter houses other than your houses until you have ˹first˺ asked permission and greeted their occupants. So a person must say, ‘Peace be upon you, may I enter?’, as is stated in one hadīth. That is better for you, than entering without permission, that perhaps you might remember (tadhakkarūna: the second tā’ ˹of tatadhakkarūna˺ has been assimilated with the dhāl) the superiority of such ˹conduct˺ and so follow it.
القرآن:٢٤:٢٧

يَا أَيُّهَا الَّذِينَ آمَنُوا لَا تَدْخُلُوا بُيُوتًا غَيْرَ بُيُوتِكُمْ حَتَّىٰ تَسْتَأْنِسُوا وَتُسَلِّمُوا عَلَىٰ أَهْلِهَا ۚ ذَٰلِكُمْ خَيْرٌ لَكُمْ لَعَلَّكُمْ تَذَكَّرُونَ  

{يا أيها الذين آمنوا لا تدخلوا بيوتا غير بيوتكم حتى تستأنوا} أي تستأذنوا {وتسلموا على أهلها} فيقول الواحد السلام عليكم أأدخل؟ كما ورد في حديث {ذلكم خير لكم} من الدخول بغير استئذان {لعلكم تذكرون} بإدغام التاء الثانية في الذال خيريته فتعملون به.

See similar narrations below:

Collected by Ḥākim
hakim:3496Abū ʿAlī al-Ḥāfiẓ > ʿAbdān al-Ahwāzī > ʿAmr b. Muḥammad al-Nāqid > Muḥammad b. Yūsuf > Sufyān > Shuʿbah > Jaʿfar b. Iyās > Mujāhid > Ibn ʿAbbās

[Machine] In His saying, "Do not enter houses other than your own until you have sought permission" [Surah An-Nur 27], He said: The writer made a mistake, it should be "until you have sought permission" (instead of "until you have made yourself known").  

الحاكم:٣٤٩٦حَدَّثَنَا أَبُو عَلِيٍّ الْحَافِظُ أَنْبَأَ عَبْدَانُ الْأَهْوَازِيُّ ثنا عَمْرُو بْنُ مُحَمَّدٍ النَّاقِدُ ثنا مُحَمَّدُ بْنُ يُوسُفَ ثنا سُفْيَانُ عَنْ شُعْبَةَ عَنْ جَعْفَرِ بْنِ إِيَاسٍ عَنْ مُجَاهِدٍ عَنِ ابْنِ عَبَّاسٍ

فِي قَوْلِهِ تَعَالَى {لَا تَدْخُلُوا بُيُوتًا غَيْرَ بُيُوتِكُمْ حَتَّى تَسْتَأْنِسُوا} [النور 27] قَالَ: أَخْطَأَ الْكَاتِبُ، حَتَّى تَسْتَأْذِنُوا  

هَذَا حَدِيثٌ صَحِيحٌ عَلَى شَرْطِ الشَّيْخَيْنِ وَلَمْ يُخْرِجَاهُ على شرط البخاري ومسلم