Salt stress seriously hinders the normal growth of plant seedling roots. Magnesium, as one of the essential medium elements for plant growth, can effectively alleviate the damage of salt stress to plant roots, but the key genes involved and their mechanism are still unclear. The purpose of this study was to explore the related molecular mechanism of exogenous magnesium regulating cation homeostasis in peanut seedlings under salt stress. Firstly, according to plant physiology experiments, it was found that exogenous magnesium treatment significantly improved the tolerance of peanut seedlings to salt stress. After that, the transcriptome data were integrated, and further gene expression analysis showed that the expression of genes such as CNGC1, NCLs, and NHX7 was regulated under exogenous magnesium treatment, which effectively reduced the accumulation of sodium ions in cells. At the same time, exogenous magnesium also regulates the expression of genes such as ACAs and POTs and maintains the homeostasis of calcium and potassium ions in cells. These results reveal the molecular mechanism of exogenous magnesium regulating the cation homeostasis of peanut seedlings under salt stress, which provides an important reference for further revealing the key genes of salt tolerance in plants.