Mg alloy suffers from its poor corrosion resistance as a result of anodic dissolution of Mg and hydrogen evolution reaction (HER) in humid environments. In this study, the effects of alloying elements (Al, Zn, Y, Ce, and Mn) on both processes in Mg alloys have been quantitatively predicted. Using first‐principle calculations, we first obtained the substitution energies of alloying elements to compare their segregation preference, and then analyzed the influence of solutes at different layers on the stability and hydrogen adsorption properties of Mg(0001) surface by calculating the formation enthalpy, surface energy, vacancy formation energy, work function, Bader charge, deformation charge density, and adsorption free energy of H atom. It has been found that, on the one hand, the interior Mn solute atoms reduce the dissolution of Mg atoms and the transfer of electrons, consequently slowing down the anodic dissolution process. On another hand, the Mn, Y, and Ce elements on the surface inhibit the cathodic HER process by elevating the absolute value of hydrogen adsorption free energy, as a result of those solutes effectively controlling H adsorption behavior on Mg(0001) surface. In contrast, all five elements dissolved inside the Mg grain do not show significant effects on the H adsorption behavior.