According to the biographical dictionary compiled by Ibn Khallikan, Muhammad ibn Tughj was born in Baghdad on 8 February 882, on the street leading to the Kufa Gate. His family was of Turkic origin from the Farghana Valley in Transoxiana, and claimed royal descent; the name of his ancestor, "Khaqan", is a Turkic royal title. Muhammad's grandfather Juff left Farghana to enter military service in the Abbasid court at Samarra, as did the father of Ibn Tulun, the founder of the Tulunid dynasty. Juff and his son, Muhammad's father Tughj, both served the Abbasids, but Tughj later entered the service of the Tulunids, who since 868 had become autonomous rulers of Egypt and Syria. Tughj served the Tulunids as governor of Tiberias (capital of the district of Jordan), Aleppo (the capital of the district of Qinnasrin) and Damascus (capital of the homonymous district). He played a major role in repelling the Qarmatian attack on Damascus in 903; although defeated in battle, he held the city itself against the Qarmatians for seven months until, with the arrival of reinforcements from Egypt, the Qarmatians were driven away. Thus Muhammad ibn Tughj spent a great part of his youth in the Tulunid Levant at his father's side, gaining his first experiences in administration—he served as his father's sub-governor of Tiberias—and war.