Resistance to anoikis is a key characteristic of many cancer cells, promoting cell survival. However, the mechanism of anoikis in hepatocellular carcinoma (HCC) remains unknown. In this study, we applied differentially expressed overlapping anoikis-related genes to classify The Cancer Genome Atlas (TCGA) samples using an unsupervised cluster algorithm. Then, we employed weighted gene coexpression network analysis (WGCNA) to identify highly correlated genes and constructed a prognostic risk model based on univariate Cox proportional hazards regression. This model was validated using external datasets from the International Cancer Genome Consortium (ICGC) and Gene Expression Omnibus (GEO). Finally, we used a CIBERSORT algorithm to investigate the correlation between risk score and immune infiltration. Our results showed that the TCGA cohorts could be divided into two subgroups, with subgroup A having a lower survival probability. Five genes (BAK1, SPP1, BSG, PBK and DAP3) were identified as anoikis-related prognostic genes. Moreover, the prognostic risk model effectively predicted overall survival, which was validated using ICGC and GEO datasets. In addition, there was a strong correlation between infiltrating immune cells and prognostic genes and risk score. In conclusion, we identified anoikis-related subgroups and prognostic genes in HCC, which could be significant for understanding the molecular mechanisms and treatment of HCC.