The Messenger of Allah ﷺ said, "Listening and obeying are required of the Muslim, whether he likes it or dislikes it, so long as he is not commanded to commit a sin. If he is commanded to commit a sin, then there is no listening and no obeying."
حدثنا مسدد حدثنا يحيى عن عبيد الله حدثني نافع
عن رسول الله ﷺ أنه قال السمع والطاعة على المرء المسلم فيما أحب وكره ما لم يؤمر بمعصية فإذا أمر بمعصية فلا سمع ولا طاعة
Bukhari narrated it in "Rulings," chapter "Hearing and obeying the ruler as long as it is not disobedience," hadith 7144, from Musaddad, and Muslim narrated it in "Leadership," chapter "The obligation of obeying rulers in matters that do not involve disobedience," hadith 1839, from the hadith of Yahya Qattan, with the same wording.