Language:
كتاب الأحكام
93  
Judgments (Ahkaam)
(4)
Chapter: To listen to and obey the Imam
(4)
باب السَّمْعِ وَالطَّاعَةِ لِلإِمَامِ مَا لَمْ تَكُنْ مَعْصِيَةً
Sahih al-Bukhari 7144

Narrated `Abdullah:

The Prophet (ﷺ) said, "A Muslim has to listen to and obey (the order of his ruler) whether he likes it or not, as long as his orders involve not one in disobedience (to Allah), but if an act of disobedience (to Allah) is imposed one should not listen to it or obey it. (See Hadith No. 203, Vol. 4)

حَدَّثَنَا مُسَدَّدٌ، حَدَّثَنَا يَحْيَى بْنُ سَعِيدٍ، عَنْ عُبَيْدِ اللَّهِ، حَدَّثَنِي نَافِعٌ، عَنْ عَبْدِ اللَّهِ ـ رضى الله عنه ـ عَنِ النَّبِيِّ صلى الله عليه وسلم قَالَ ‏ "‏ السَّمْعُ وَالطَّاعَةُ عَلَى الْمَرْءِ الْمُسْلِمِ، فِيمَا أَحَبَّ وَكَرِهَ، مَا لَمْ يُؤْمَرْ بِمَعْصِيَةٍ، فَإِذَا أُمِرَ بِمَعْصِيَةٍ فَلاَ سَمْعَ وَلاَ طَاعَةَ ‏"‏‏.‏
Reference : Sahih al-Bukhari 7144
In-book reference : Book 93, Hadith 8
USC-MSA web (English) reference : Vol. 9, Book 89, Hadith 258
  (deprecated numbering scheme)