WASHINGTON — As President Donald Trump began losing confidence in national security adviser John Bolton, whom he fired on Tuesday, he reached out to the man he had fired to give Bolton the job: retired Lt. Gen. H.R. McMaster.

In phone calls to McMaster — the first of which took place last fall — Trump told his second national security adviser that he missed him, according to two people familiar with the conversations. It’s a sentiment the president has also expressed to White House aides, they said. Trump has solicited McMaster's advice on various national security challenges, even asking McMaster whom he should nominate to lead the Pentagon, they said.

Trump's contacts with McMaster perhaps presaged his decision Tuesday to unceremoniously fire Bolton. They also were a remarkable shift for the president that is emblematic of how much Bolton fell out of favor since Trump welcomed him into the White House 17 months ago. At that time, Trump was barely speaking to McMaster and regularly did derogatory impressions of him in his absence, according to current and former White House officials.

Download the NBC News app for breaking news and politics

Like Bolton, McMaster’s firing was also hasty and announced publicly by the president on Twitter. Bolton later said he had resigned and was not fired. Trump's patience for Bolton ultimately wore thin; aides say Trump was close to firing Bolton earlier this year, even putting his name on a list of officials he'd like to get rid of before the end of the year, after which such moves might have a negative effect on his re-election campaign, officials said.

The president was angered by what he viewed as Bolton's positioning himself in the news media as the decision-maker on key issues like Iran and Venezuela, the officials said, and it didn't help that Trump thought those policies weren't working.

Trump's first national security adviser, retired Lt. Gen. Michael Flynn, was in a Virginia courtroom Tuesday for a hearing ahead of his sentencing in December. Flynn pleaded guilty in December 2017 to a charge of lying to the FBI about his conversations with the Russian ambassador.

In late May or early June, Bolton was told by a White House official that he should steer clear of the president, keep a low profile and let him cool off amid tensions between the two over policy, according to three people familiar with the conversation. The official suggested Bolton travel more or otherwise find matters to focus on that would keep him away from Trump.

A spokesman for Bolton denied such a conversation took place and dismissed the notion of tensions with the president or any other administration officials.

When asked about the conversation, one White House official described the talk as "good advice."

When Trump is unsatisfied with a top aide, he sometimes turns to that aide's predecessor, with whom he was at odds and had fired. For example, Trump began calling his first chief of staff, Reince Priebus, when his second one, John Kelly, started trying to get him to do things he didn't want to do.

Trump's phone calls to McMaster began in the fall of 2018, about six months after Bolton had taken the job. The most recent call that the people familiar with the conversations knew of was a few months ago.

In another conversation in late spring, when Trump was unsure about his choice of then-Acting Defense Secretary Patrick Shanahan to permanently hold the job, the president wanted to know what McMaster thought he should do. At the time, Trump was asking his advisers inside and outside the White House for alternatives to Shanahan, and he ultimately switched to nominate Mark Esper, who was confirmed by the Senate.

At least one of Trump's calls with McMaster focused on Iran, an issue over which Trump and Bolton have clashed because the president felt his national security adviser was pushing him into a military confrontation. Bolton has long been one of the Republican Party's leading voices on taking an aggressive approach on Iran, including advocating for regime change. He was instrumental in Trump's decision to withdraw from the Iran nuclear deal and has advocated positions that have at times put him at odds with Trump, who campaigned on ending, not starting, military conflicts overseas.

McMaster declined to comment.

Perhaps most detrimental for Bolton, officials said, Trump blamed him for a public narrative that the national security adviser was shaping Iran policy, not the president.

"The president's view is: 'I run Iran. John may think he runs Iran, but I run Iran,'" a former White House official said.

Trump and Bolton also have differed over North Korea. Just weeks into his tenure, Bolton angered the president by suggesting a denuclearization deal with North Korea could be modeled after the one the U.S. had achieved with Libya. Libya's leader, Muammar Gaddafi, was killed eight years later after the U.S. intervened to prevent his slaughter of pro-democracy demonstrators.

The comparison of Libya and North Korea by Bolton — who has called for regime change in Pyongyang — didn't go over well with North Korean leader Kim Jong Un, whom Trump was arranging to meet for the first time. The president tried to clarify, saying, "the Libya model isn't the model that we have at all when we're thinking of North Korea."

Bolton took a far different approach to the job than McMaster did. While McMaster held meetings and ran a policy process that involved meetings with officials from other agencies, Bolton opted to work policy directly with the president. He hasn't chaired a principals' committee meeting at the National Security Council in months, according to three people familiar with the matter. His strong opinions and tensions with Trump have made him increasingly isolated in the West Wing, with few allies in the administration.

Still, the president's outreach to McMaster surprised the people closest to him. "He must be getting pretty far down on his call list because McMaster is not at the top," another former White House official said.

By the time McMaster left the White House it had become an open secret that Trump had stopped having him brief him because he didn't like his style and would cut him off in meetings or exclude him altogether. Yet while the two weren't close, officials said the president didn't have the sort of animosity toward McMaster that he'd developed for others on his national security team, such as former Secretary of State Rex Tillerson, who had called Trump a "moron."

"I never got the impression that Trump hated McMaster. He just made fun of him. He was the general in the suits that didn't fit, that were off the rack, who didn't understand business," the former White House official said.

"He didn't take him seriously," the official added. "But there never was this sort of hostility like there was with a Tillerson."

A senior administration official said that the president supported Bolton's recent trip to London for meetings with British officials largely focused on the U.K.'s plan to exit the European Union this fall. But as Bolton began his talks with officials there, the president called the new prime minister, Boris Johnson, a move that made clear who is in charge.