It is obligatory to be obedient to the ruler except in sinful matters

1204. Narrated Aboo Huraira (radhi Allaahu anhu) : Allaah’s Messenger (Sallallaahu Alaihi Wasallam) said,

Whoever obeys me, he obeys Allaah, and whoever disobeys me, he disobeys Allaah, and whoever obeys my ruler (the ruler I appoint) he obeys me, and whoever disobeys my (appointed) ruler, he disobeys me.

[Saheeh Al-Bukhaari, Hadeeth No. 251, Vol. 9]

Source : The Book Of Al-Imaara – Chapter 8 – It is obligatory to be obedient to the ruler except in sinful matters.  Al-Lulu Wal-Marjan,Compiled by : Fuwad Abdul-Baqi,Translated by : Dr. Muhammad Muhsin Khan

Obligation of Obedience to the Ruler in what is Lawful and Prohibition of Obeying them in what is Unlawful
Riyad-us-Saliheen