In a brief statement to announce the appointment, the Madrid giants said Lopetegui had been appointed on a three-year contract.
Lopetegui, 51, was hired by Spain following their underwhelming Euro 2016 campaign and the former Porto boss has helped re-establish La Roja as genuine contenders this summer in Russian Federation.
The move comes as a surprise, with Lopetegui not featuring prominently among the host of managers linked with the role since the departure of Zinedine Zidane days after Madrid's 3-1 victory over Liverpool in the Champions League final.
Lopetegui will take the reigns after coaching Spain in the World Cup, with the 51-year-old agreeing a three-year deal with the La Liga side.
Rafael Nadal ready for 'tough final' against Dominic Thiem on Sunday
The pivotal part of the match was the second-set tiebreaker in which both men played superbly - and both had chances to end it. The boys' final at the French Open will pit top-seeded Sebastien Baez of Argentina against Chun Hsin Tseng from Taiwan.
As a player Lopetegui was a goalkeeper who spent spells as a backup at both Madrid and Barcelona, also playing for Rayo Vallecano and CD Logrones.
Lopetegui returned to club management with Porto in 2014, but he was sacked midway through his second season after failing to deliver silverware, leaving the club in January 2016 with the club third in the league, and out of the Champions League.
They face a World Cup group containing Iberian rivals Portugal, as well as Morocco and Iran.
He began coaching in 2003 as assistant manager to Spain's Under-17 team and was also in charge of the Under-19s, Under-20s and Under-21s, before moving to Porto.