Lithium extraction from salt-lake brines by membrane separation has become a trend to satisfy the global increasing market demand of lithium. Although some progresses have been achieved in membrane fabrication, traditional theories like steric hindrance and Donnan effect struggle to comprehensively explain why Li+/Mg2+ separation factor and Li + permeation could be simultaneously improved, which is also one of the key challenge for this technology. By using uniform nanopores in covalent organic frameworks as the model system, we herein demonstrate that the compensatory effect of pore-wall groups on ionic hydration plays a significant role in Li+/Mg2+ separation: compensation to Li+ second hydration shells is beneficial to Li+ permeation while that to first shells is counterproductive; meanwhile less compensation to Mg2+ hydration is more advantageous to rejecting Mg2+. Via non-equilibrium molecular dynamics simulations, moderately-electropositive nanopores are discovered to be capable of simultaneously enhancing separation factor and Li+ permeation. Balanced ratio of hydrophilic and positively-charged groups on pore walls ensures the nanopores possess suitable compensatory effects that can accelerate Li + permeation while rejecting Mg2+. In contrast, hydrophobic, hydrophilic and strongly-electropositive nanopores are not competent due to their excessively weak or strong compensatory effects. The findings and understandings can be also applicable to other kinds of nanoporous materials and help to inspire other ion-separation applications.