Allah the Exalted said:
١٤٠ - باب الاستئذان وآدابه
Allah the Exalted said:
O you who have believed, do not enter houses other than your own houses until you ascertain welcome and greet1 their inhabitants. That is best for you; perhaps you will be reminded [i.e., advised].
And the Exalted also said:
And when the children among you reach puberty, let them ask permission [at all times] as those before them have done. Thus does Allāh make clear to you His verses; and Allāh is Knowing and Wise.
Abū Mūsā al-Ashʿarī (may Allah be pleased with him) reported: The Messenger of Allah ﷺ said: "Asking permission to enter is three times; if you are granted permission, then [enter], and if not, then go back." Agreed upon (al-Bukhārī and Muslim).
Sahl ibn Saʿd (may Allah be pleased with him) reported: The Messenger of Allah ﷺ said: "Asking permission to enter was ordained only on account of the gaze." Agreed upon (al-Bukhārī and Muslim).
Ribʿī ibn Ḥirāsh reported: A man from Banū ʿĀmir narrated to us that he sought permission to enter upon the Prophet ﷺ while he was in a house, and he said: "May I come in?" So the Messenger of Allah ﷺ said to his servant: "Go out to this man and teach him how to ask permission; say to him: 'Say: Peace be upon you—may I enter?'" The man heard him and said: "Peace be upon you—may I enter?" So the Prophet ﷺ granted him permission, and he entered. Narrated by Abū Dāwūd with a sound chain (isnād).
Kildah ibn al-Ḥanbal (may Allah be pleased with him) reported: I came to the Prophet ﷺ and entered upon him without giving the greeting of peace, so the Prophet ﷺ said: "Go back and say: 'Peace be upon you—may I enter?'" Narrated by Abū Dāwūd and al-Tirmidhī, who said: "A good (ḥasan) hadith."