After Brazilian teenager Rodrygo tapped in, Toni Kroos doubled the tally with a clinical finish about 90 seconds later.
Skipper Sergio Ramos extended the lead with a retaken penalty after his initial effort was saved.
Karim Benzema rolled in another penalty after the break before Luka Jovic opened his Real account with a header.
Zinedine Zidane’s side, who did not play at the weekend after El Clasico was postponed because of civil unrest in Catalonia, made the perfect response to losing at Malaga in their previous league outing.
Madrid started at a high tempo against their city rivals, who earned their first league win of the season against Malaga on Saturday following former Southampton manager Mauricio Pellegrino’s departure, and there was little doubt about the outcome once once they moved 2-0 ahead after just eight minutes.
Skipper Ramos was rather fortunate to be allowed another chance to score from the spot when Leganes goalkeeper Juan Soriano was adjudged to have moved off his line before saving a weak first attempt.
Although Leganes rallied after the break, the home side always had complete control and two more goals in the final 20 minutes gave a fairer reflection of their overall dominance.
Benzema, who set up the first two goals, deservedly got on the scoresheet himself before his second-half replacement Jovic headed in a pinpoint cross from Dani Carvajal in stoppage time.