Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
bukhari:2955Musaddad > Yaḥyá > ʿUbaydullāh > Nāfiʿ > Ibn ʿUmar

The 'Prophet said, "It is obligatory for one to listen to and obey (the ruler's orders) unless these orders involve one disobedience (to Allah); but if an act of disobedience (to Allah) is imposed, he should not listen to or obey it."  

البخاري:٢٩٥٥حَدَّثَنَا مُسَدَّدٌ حَدَّثَنَا يَحْيَى عَنْ عُبَيْدِ اللَّهِ قَالَ حَدَّثَنِي نَافِعٌ عَنِ ابْنِ عُمَرَ ؓ

عَنِ النَّبِيِّ ﷺ وَحَدَّثَنِي مُحَمَّدُ بْنُ صَبَّاحٍ حَدَّثَنَا إِسْمَاعِيلُ بْنُ زَكَرِيَّاءَ عَنْ عُبَيْدِ اللَّهِ عَنْ نَافِعٍ عَنِ ابْنِ عُمَرَ ؓ عَنِ النَّبِيِّ ﷺ قَالَ السَّمْعُ وَالطَّاعَةُ حَقٌّ مَا لَمْ يُؤْمَرْ بِالْمَعْصِيَةِ فَإِذَا أُمِرَ بِمَعْصِيَةٍ فَلاَ سَمْعَ وَلاَ طَاعَةَ