Organisms maintain the correct balance of intracellular metals primarily through metal-sensing proteins that control transport and storage of the target ion(s). Here, we reveal the basis of metal sensing and genetic control by a metalloregulatory RNA. Our data demonstrate that a previously uncharacterized orphan riboswitch, renamed the "M-box," is a divalent metal-sensing RNA involved in Mg2+ homeostasis. A combination of genetic, biochemical, and biophysical techniques demonstrate that Mg2+ induces a compacted tertiary architecture for M-box RNAs that regulates the accessibility of nucleotides involved in genetic control. Molecular details are provided by crystallographic structure determination of a Mg2+-bound M-box RNA. Given the distribution of this RNA element, it may constitute a common mode for bacterial metal ion regulation, and its discovery suggests the possibility of additional RNA-based metal sensors in modern and primordial organisms.