This article explores the social structure of the Erhai region, Yunnan, in terms of the cultural meanings of surnames and ancestors. The main argument is that these cultural meanings were both a strategic response to external pressure and an integral part of social transformations that occurred in the Nanzhao and Dali kingdoms (752–1254). A review of the textual evidence on the Nanzhao and Dali kingdoms from before the fifteenth century indicates that Buddhist and local legends were central to the codification of royal power in these kingdoms and that surnames played an important role in that codification. First, the state used surnames as political symbols to integrate different social groups, while the discourse of ancestors associated with these surnames continued to express the multiple layers of identity of these surname groups as they were merged together. Second, the state strengthened the power of the surnames by merging their ancestors with the Buddhist legends of Asóka and Guanyin, and integrating Buddhism more generally into state organization. Third, male ancestors were identified as monks who held important state positions, to which their descendants laid claim, while female ancestors were understood as an important source by which male ancestors were able to obtain political power. In sum, whereas the patrilineal features of surnames legitimized state expansion, the matrilineal features of surnames legitimated hierarchy. In the Nanzhao and Dali kingdoms a Buddhist worldview unified the nobility in the political sphere and the ritual specialists in the religious sphere. At the same time this worldview reflected and legitimized a hierarchical social formation that brought together diverse social groupings within a single unified classification of surnames and statuses.