e lived with al-Mowahideen. He moved back and forth through Algeria and settled in Biskra. 
At the same time, in Morocco, Sultan Abu Enan, who had recently settled on the throne of his father, was on his way to conquer Algeria. Ibn Khaldun traveled to Tlemcen to meet the sultan, and mentions that the sultan honored him and sent him with his chamberlain Ibn Abi Amr to Bougie to witness its submission to Sultan Abu Enan.
Ibn Khaldun stayed in the company of the chamberlain while the sultan moved back to the capital, Fez. In 1354 A.C.E. (755 A.H.) Ibn Khaldun accepted the invitation to join the council of ‘ulama and moved to Fez. He was eventually promoted to the post of seal-bearer and accepted it reluctantly, as it was inferior to the posts once occupied by his ancestors.
Ibn Khaldun used his stay in Fez to further his studies. Fez at this time was a capital of Morocco and enjoyed the company of many scholars from all over North Africa and Andalusia. Ibn Khaldun was an ambitious young man and at this point of his life he began to engage in court politics. He was promoted from one position to another.  He also conspired with Abu Abdullah Muhammad, the dethroned ruler of Bougie who was captive in Fez at that time. Abu Abdullah was from the Banu Hafs that were patrons of Ibn Khaldun’s family. Sultan Abu Enan found out about the conspiracy and imprisoned Ibn Khaldun. Abu Abdullah was released from prison and Ibn Khaldun lingered on for two years. Sultan Abu Enan fell ill and died before fulfilling his promise to release Ibn Khaldun. The Wazir al-Hassan ibn Omar ordered the release of Ibn Khaldun, who was then restored to his former position. 
The political climate was tense and Ibn Khaldun again tested his fate and conspired against the wazir with al-Mansur. This loyalty was short-lived as well. He conspired with Sultan Abu Salem who overthrew al-Mansur. Ibn Khaldun took the position of secretary (literally, “repository of secrets” Amin as-Sir). 
Here Ibn Khaldun excelled in his position and composed many poems. He occupied 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 