The 51-year-old specialist has agreed on a three-year deal with the 33-time La Liga champions who have been without a manager following Zinedine Zidane's resignation on May 31.
Under the Frenchman's management, Real claimed an unprecedented three Champions League titles in a row.
Lopetegui led the Red Fury to the 2018 FIFA World Cup finals without suffering a single defeat in their ten World Cup qualifying matches and recently extended his contract with the Spanish Football Federation until 2020.
Before taking charge of the senior Spanish team, Lopetegui worked with the Spanish youth national teams winning the European Under-19 Championship in 2012 and the Under-21 Championship in 2013.
Spain has been drawn in Group B and will play reigning European champions Portugal in Sochi on June 15, Iran in Kazan on June 20 and Morocco in Kaliningrad on June 25.