All-atom simulations have become increasingly popular to study conformational and dynamical properties of nucleic acids as they are accurate and provide high spatial and time resolutions. This high resolution, however, comes at a heavy computational cost, and, within the time scales of simulations, nucleic acids weakly fluctuate around their ideal structure exploring a limited set of conformations. We introduce the RBB-NA algorithm (available as a package in the Open Source Library PLUMED), which is capable of controlling rigid base parameters in all-atom simulations of nucleic acids. With suitable biasing potentials, this algorithm can "force" a DNA or RNA molecule to assume specific values of the six rotational (tilt, roll, twist, buckle, propeller, opening) and/or the six translational parameters (shift, slide, rise, shear, stretch, stagger). The algorithm enables the use of advanced sampling techniques to probe the structure and dynamics of locally strongly deformed nucleic acids. We illustrate its performance showing some examples in which DNA is strongly twisted, bent, or locally buckled. In these examples, RBB-NA reproduces well the unconstrained simulations data and other known features of DNA mechanics, but it also allows one to explore the anharmonic behavior characterizing the mechanics of nucleic acids in the high deformation regime.