Increasing evidence have revealed a positive correlation between gut microbiota and shrimp health, in which a healthy shrimp gut consists of a complex and stable microbial community. Given that both abiotic and biotic factors constantly regulate shrimp gut microbiota, any changes affecting the levels of these factors could cause modification to the gut microbiota assemblage. The goal of this study was to explore the effects of salinity levels and pathogenic Vibrio harveyi infection on the diversity, structure, composition, interspecies interaction, and functional pathways of Litopenaeus vannamei gut microbiota. Juvenile shrimp were cultured at 5 ppt, 20 ppt, and 30 ppt for two months prior to Vibrio harveyi infection. After pathogenic V. harveyi challenge test, genomic DNA was isolated from the shrimp gut, and subjected to the 16S rRNA metagenomic sequencing analysis. We observed that gut microbiota diversity of shrimp cultured at 5 ppt and 30 ppt were lower than those cultured at 20 ppt after exposure to V. harveyi infection, suggesting that shrimp cultured at the two former salinity levels were more susceptible to V. harveyi infection. Network analysis also showed that shrimp cultured at 20 ppt exhibit a more stable bacterial network with complex interspecies interaction, even after induced by V. harveyi. Moreover, the presence of a high number of beneficial bacteria such as Pseudoruegeria, Rhodovulum, Ruegeria, Shimia and Lactobacillus in shrimp cultured at 20 ppt might have played a role in inhibiting the growth of V. harveyi and other potentially pathogenic bacteria. Besides, bacterial functional pathway prediction has also shown that metabolic pathways such as phenylalanine metabolism, glycine, serine and threonine metabolism, starch and sucrose metabolism, glyoxylate and dicarboxylate metabolism, carbon metabolism and biofilm formation process were significantly higher in shrimp cultured at 20 ppt. Collectively, our results suggested that 20 ppt is an optimal salinity that suppresses the growth of V. harveyi and potential pathogenic bacteria in the shrimp gut, which could possibly minimize the risk of pathogenic infection for sustainable production of healthy shrimp.