كتاب الإمارة
33  
The Book on Government
(8)
Chapter: The obligation of obeying leaders in matters that do not involve sin, but it is forbidden to obey them in sinful matters
(8)
باب وُجُوبِ طَاعَةِ الأُمَرَاءِ فِي غَيْرِ مَعْصِيَةٍ وَتَحْرِيمِهَا فِي الْمَعْصِيَةِ ‏‏
Sahih Muslim 1834

It has been narrated on the authority of Ibn Juraij that the Qur'anic injunction:

" 0 you who believe, obey Allah, His Apostle and those in authority from amongst You" (iv. 59) -was revealed in respect of 'Abdullah b. Hudhafa b. Qais b. Adi al-Sahmi who was despatched by the Prophet (ﷺ) as leader of a military campaign. The narrator said: He was informed of this fact by Ya'la b. Muslim who was informed by Sa'id b. Jubair who in turn was informed by Ibn Abbas.
حَدَّثَنِي زُهَيْرُ بْنُ حَرْبٍ، وَهَارُونُ بْنُ عَبْدِ اللَّهِ، قَالاَ حَدَّثَنَا حَجَّاجُ بْنُ مُحَمَّدٍ، قَالَ قَالَ ابْنُ جُرَيْجٍ نَزَلَ ‏{‏ يَا أَيُّهَا الَّذِينَ آمَنُوا أَطِيعُوا اللَّهَ وَأَطِيعُوا الرَّسُولَ وَأُولِي الأَمْرِ مِنْكُمْ‏}‏ فِي عَبْدِ اللَّهِ بْنِ حُذَافَةَ بْنِ قَيْسِ بْنِ عَدِيٍّ السَّهْمِيِّ بَعَثَهُ النَّبِيُّ صلى الله عليه وسلم فِي سَرِيَّةٍ ‏.‏ أَخْبَرَنِيهِ يَعْلَى بْنُ مُسْلِمٍ عَنْ سَعِيدِ بْنِ جُبَيْرٍ عَنِ ابْنِ عَبَّاسٍ ‏.‏
Reference : Sahih Muslim 1834
In-book reference : Book 33, Hadith 45
USC-MSA web (English) reference : Book 20, Hadith 4517
  (deprecated numbering scheme)