On Thursday, however, Mr. Trump summoned him to the Oval Office to discuss the job. Hours later, Mr. Bolton was on Fox, where he has been an analyst, for a pre-scheduled interview, in which he confessed surprise at how quickly Mr. Trump announced the appointment. “This hasn’t sunk in,” he said.

The news of the appointment competed with an exclusive interview on CNN of a former Playboy model, Karen McDougal, who described to Anderson Cooper what she said was a 10-month sexual relationship with Mr. Trump in 2006. Mr. Trump has denied the affair.

In his interview on Fox News, Mr. Bolton declined to discuss his views on Iran, Russia or North Korea, though he acknowledged his positions were hardly a mystery after years of writing and speaking. He described the job of national security adviser as making sure that the bureaucracy did not impede the decisions of the president.

Officials said that General McMaster’s departure was a mutual decision and amicable, with little of the recrimination that marked Mr. Tillerson’s exit. They said it was not related to a leak on Tuesday of briefing materials for Mr. Trump’s phone call with President Vladimir V. Putin of Russia, which infuriated the president and did not help General McMaster’s case. Mr. Bolton complained on Fox News that “a munchkin in the executive branch” was responsible for the leak and called it “completely unacceptable.’’

Mr. Trump issued a statement that coincided with his tweet. “H. R. McMaster has served his country with distinction for more than 30 years,” the statement said. “He has won many battles and his bravery and toughness are legendary. General McMaster’s leadership of the National Security Council staff has helped my administration accomplish great things to bolster America’s national security.”

General McMaster said in a telephone interview on Thursday that his departure had been under discussion for weeks, and, “really, the only issue that had been left open is timing.” He would have preferred to stay in the West Wing until the summer, but the timing was dictated by “what was best for him and the country,” he said, referring to the president.