The Prophet ﷺ said, "Listening and obeying are an obligation upon the Muslim in what he likes and what he dislikes, as long as he is not commanded to commit a sin. But if he is commanded to commit a sin, then there is no listening and no obeying."
عن النبي ﷺ قال: السمع والطاعة على المرء المسلم فيما أحب وكره ما لم يؤمر بمعصية، فإذا أمر بمعصية فلا سمع ولا طاعة.
Bukhari related it in Ahkam (7144), and Muslim in Imarah (1839: 38), both through the route of Ubaydullah, who said, Nafi narrated to me, from Ibn Umar, who mentioned it.