Organisms must maintain physiological levels of Mg(2+) because this divalent cation is critical for the stabilization of membranes and ribosomes, for the neutralization of nucleic acids, and as a cofactor in a variety of enzymatic reactions. In this review, we describe the mechanisms that bacteria utilize to sense the levels of Mg(2+) both outside and inside the cytoplasm. We examine how bacteria achieve Mg(2+) homeostasis by adjusting the expression and activity of Mg(2+) transporters and by changing the composition of their cell envelope. We discuss the connections that exist between Mg(2+) sensing, Mg(2+) transport, and bacterial virulence. Additionally, we explore the logic behind the fact that bacterial genomes encode multiple Mg(2+) transporters and distinct sensing systems for cytoplasmic and extracytoplasmic Mg(2+). These analyses may be applicable to the homeostatic control of other cations.