The Messenger of Allah ﷺ said, "Listening and obeying are obligatory upon every Muslim, in what he likes and what he dislikes, 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."
حدثنا قتيبة حدثنا الليث عن عبيد الله بن عمر عن نافع
قال رسول الله ﷺ السمع والطاعة على المرء المسلم فيما أحب وكره ما لم يؤمر بمعصية فإن أمر بمعصية فلا سمع عليه ولا طاعة
Muslim also narrated it, in the book of Leadership, chapter on the obligation of obeying rulers in matters other than sin, and the prohibition of obeying them in sin, hadith no. 1839, from Qutayba, with the same wording.On this subject there are also narrations from Ali [Bukhari, no. 4340, 7145, and Muslim, no. 1840], Imran bin Husayn [Ahmad, 4/426-427], and Hakam bin Amr Ghifari [Ahmad, 5/66].