cupied the position for two more years and was then appointed Chief Justice. He showed great ability in this position, however, due to constant rivalry with high officials, he lost favor with the sultan. 
However, this proved unimportant, when a revolt took place and Sultan Abu Salem was overthrown by Wazir Omar. Ibn Khaldun sided with the victors and was reinstated to his post, with higher pay. Ibn Khaldun was as ambitious as ever, and wanted a higher position, namely that of chamberlain. For reasons unknown, perhaps he was not trusted, he was refused the position. This upset him enough that he resigned his position, and he in turn upset the wazir. Ibn Khaldun asked to leave Fez and go back to Tunisia and was refused. It was then that he asked the wazir’s son-in-law to intercede on his behalf, that he be allowed to go to Andalusia. 
Sultan Muhammad al-Ahmar, the King of Granada, was deposed by his brother Ismail who was supported by his brother-in-law. Sultan Muhammad was a friend of Sultan Abu Salem, who had helped Ibn Khaldun when he was deported to Andalusia by Sultan Abu Enan. When Sultan Abu Enan died and Sultan Abu Salem became ruler, that friendship was rekindled. Furthermore, when Ismail al-Ahmar was declared King of Granada in a palace revolt, Sultan Muhammad took refuge in Morocco with Sultan Abu Salem. They were welcomed with great fanfare, and Ibn Khaldun was present at the festivities. Among Sultan Muhammad’s party was his wise Wazir Ibn al-Khatib, who developed a close friendship with Ibn Khaldun. 
Sultan Muhammad attempted to restore his throne in Granada through an agreement with Pedro the Cruel, King of Castile. Pedro delayed the execution of the agreement upon hearing of Sultan Abu Salem’s death. Sultan Muhammad appealed to Ibn Khaldun for assistance from Wazir Omar. Ibn Khaldun used his influence to help him, and Ibn Khaldun was even entrusted to care for Sultan Muhammad’s family in Fez. The wazir granted Sultan Muhammad Ronda and the surrounding country. Sultan Muhammad continued his efforts and recaptured his throne in 1361 A.C.E. (763 A.H.). He then recalled his Wazir Ibn al-Khatib. 
When the relationship between Sultan Muhammad and Ibn Khaldun turned sour and uncertain, he turned toward Andalusia. He was welcomed and honored by Sultan Muhammad, who admitted him to his private council. In the following year, Sultan Muhammad sent Ibn Khaldun on an ambassadorial mission to Pedro, the King of Castile. Ibn Khaldun concluded the mission and peaceful terms were established between them. Pedro offered Ibn Khaldun a position in his service and the return of his family’s former estate at Castile. Ibn Khaldun declined the offer. 
Upon his return from Castile, Ibn Khaldun offered Pedro’s gift to him to the sultan and in return, the sultan gave him the village of Elvira. Soon Ibn Khaldun was restless once more and in the following year, 1364 A.C.E. (766 A.H.), when he received an invitation from his friend Abu Abdullah, who had recaptured his throne at Bougie, Ibn Khaldun left Granada after asking permission to leave from Sultan Muhammad. 
Ibn Khaldun arrived in Bougie at the age of thirty-two. His plans had finally been realized. The period of imprisonment in Fez did not go to waste. He entered the city as a favorite guest. He accepted the position of hajib for Emir Muhammad. However, his life of power did not last long, as in the following year Abul Abbas killed the Emir Muhammad, his cousin. Ibn Khaldun handed the city to him and retired to the city of Biskra. He continued his political work in relaying the tribes to the service of this or that emir or sultan. He continued his practice of shifting loyalties as times and opportunities afforded him. He finally retired to a far outpost south of Constantine, Fort Salama. 
In Fort Salama, at the age of forty-five, he enjoyed a peaceful existence, and began to write his famous Muqqddimah, and the first version of his Universal History.
He dedicated his work to the current Emir of Constantine, Sultan Abul Abbas. But tranquility did not last long with Ibn Khaldun, 