Abstract Introduction The study of bacterial diversity in human samples is crucial for developing biomarkers of health and disease. This research characterized the taxonomic and functional diversity of the anorectal bacterial microbiota in men who hae sex with men (MSM) with HIV compared to men from this group without HIV. Materials and Methods In July and August 2023, self‐collected anorectal swabs were obtained. DNA was extracted from each sample, and metagenomic sequencing was performed. With the obtained data, alpha and beta diversity, bacterial abundance, differential operational taxonomic units, and functional diversity were determined. Results Initially, 90 samples were collected, with 20 discarded due to having less than 200 ng of DNA and 15 due to incomplete sequencing, leaving 55 samples analysed (15 HIV‐positive and 40 HIV‐negative). No significant differences were found between groups in terms of alpha diversity (Shannon index p = 0.45) and beta diversity (PERMANOVA R = −0.03). Prevotella was identified as the most abundant genus in both groups. Twelve genes were found to be more abundant in the anorectal microbiota of the HIV group, which promote bacterial growth, colonization and survival. Conclusion Alterations in the anorectal microbiota could influence the pathogenesis of HIV and its complications in this population, underscoring the need to investigate these mechanisms and explore interventions to improve health. Longitudinal studies are needed to analyse changes in the anorectal microbiota during HIV infection and its response to treatment, integrating metagenomic, clinical, and immunological data to better understand the interactions between HIV, the microbiota and host health.