Objective To investigate the effect of berberine on gut microbiota and T helper cell 17 (Th17), regulatory T cell (Treg) cell in sleep deprived rats.Methods SD rats were randomized into control group, model group, low-dose and high-dose group (BBR1 and BBR2, 100 mg/kg and 200 mg/kg, administrated orally).Sleep deprived rat model was established by the small-platforms-over-water method.The number of bacteria in rectum content of rat was detected.The ratio of Th17/Treg was evaluated by flow cytometry.The expression of interleukin 17 (IL-17), RAR-related orphan receptor (ROR) C, and Forkhead Box Protein P3 (Foxp3) mRNA was evaluated.Results The counting of Clostridium perfringens was elevated (P<0.05), the amount of other microbiota decreased (P<0.05), Th17/Treg ratio(P<0.05), IL-17 and RORC expression enhanced (P<0.05), Foxp3 expression decreased (P<0.05) in the gut of model rats.In contrast, treatment with berberine inhibited the proliferation of Clostridium perfringens(P<0.05), and promoted the growth of other microbiota (P<0.05), and dampened Th17/Treg ratio (P<0.05), regressed IL-17 and RORC expression, augmented Foxp3 expression.Conclusions Various doses of berberine can counteract gut microbiota imbalance and Th17/Treg imbalance induced by sleep deprivation.