WASHINGTON — Back when their relationship was fresh and new, and President Trump still called his defense secretary “Mad Dog” — a nickname Jim Mattis detests — the wiry retired Marine general often took a dinner break to eat burgers with his boss in the White House residence.
Mr. Mattis brought briefing folders with him, aides said, to help explain the military’s shared “ready to fight tonight” strategy with South Korea, and why the North Atlantic Treaty Organization has long been viewed as central to protecting the United States. Using his folksy manner, Mr. Mattis talked the president out of ordering torture against terrorism detainees and persuaded him to send thousands more American troops to Afghanistan — all without igniting the public Twitter castigations that have plagued other national security officials.
But the burger dinners have stopped. Interviews with more than a dozen White House, congressional and current and former Defense Department officials over the past six weeks paint a portrait of a president who has soured on his defense secretary, weary of unfavorable comparisons to Mr. Mattis as the adult in the room, and increasingly concerned that he is a Democrat at heart.
Nearly all of the officials, as well as confidants of Mr. Mattis, spoke on condition of anonymity to discuss the internal tensions — in some cases, out of fear of losing their jobs.
In the second year of his presidency, Mr. Trump has largely tuned out his national security aides as he feels more confident as commander in chief, the officials said. Facing what is likely to be a heated re-election fight once the 2018 midterms are over, aides said Mr. Trump was pondering whether he wanted someone running the Pentagon who would be more vocally supportive than Mr. Mattis, who is vehemently protective of the American military against perceptions it could be used for political purposes.
White House officials said Mr. Mattis had balked at a number of Mr. Trump’s requests. That included initially slow-walking the president’s order to ban transgender troops from the military and refusing a White House demand to stop family members from accompanying troops deploying to South Korea. The Pentagon worried that doing so could have been seen by North Korea as a precursor to war.
Over the last four months alone, the president and the defense chief have found themselves at odds over NATO policy, whether to resume large-scale military exercises with South Korea and, privately, whether Mr. Trump’s decision to withdraw the United States from the Iran nuclear deal has proved effective.
The arrival at the White House earlier this year of Mira Ricardel, a deputy national security adviser with a history of bad blood with Mr. Mattis, has coincided with new assertions from the West Wing that the defense secretary may be asked to leave after the midterms.
Mr. Mattis himself is becoming weary, some aides said, of the amount of time spent pushing back against what Defense Department officials think are capricious whims of an erratic president.
The defense secretary has been careful to not criticize Mr. Trump outright. Pentagon officials said Mr. Mattis had bent over backward to appear loyal, only to be contradicted by positions the president later staked out. How much longer Mr. Mattis can continue to play the loyal Marine has become an open question in the Pentagon’s E Ring, home to the Defense Department’s top officials.
The fate of Mr. Mattis is important because he is widely viewed — by foreign allies and adversaries but also by the traditional national security establishment in the United States — as the cabinet official standing between a mercurial president and global tumult.
“Secretary Mattis is probably one of the most qualified individuals to hold that job,” Senator Jack Reed of Rhode Island, the top Democrat on the Senate Armed Services Committee, said in an interview. A Mattis departure from the Pentagon, Mr. Reed said, “would, first of all, create a disruption in an area where there has been competence and continuity.”
But that very sentiment is part of a narrative the president has come to resent.
The one-two punch last week of the Bob Woodward book that quoted Mr. Mattis likening Mr. Trump’s intellect to that of a “fifth or sixth grader,” combined with The New York Times Op-Ed by an unnamed senior administration official who criticized the president, has fueled Mr. Trump’s belief that he wants only like-minded loyalists around him. (Mr. Mattis has denied comparing his boss to an elementary school student and said he did not write the Op-Ed.)
Mr. Trump, two aides said, wants Mr. Mattis to be more like Secretary of State Mike Pompeo, a political supporter of the president. During a televised June 21 cabinet meeting, held as migrant children were being separated from their parents at the southwestern border, Mr. Mattis and Mr. Pompeo were a study of contrasts: On the president’s left, the defense secretary sat stone-faced; on his right, the secretary of state was chuckling at all of Mr. Trump’s jokes.
Getting Mr. Mattis to abandon the apolitical stand he has clung to his entire life will be next to impossible, his friends and aides said.
Mr. Mattis has assiduously avoided the limelight during his tenure because he is fearful, aides said, about being put on the spot by questions that will expose differences with his boss. He has batted down multiple requests from the White House to go on “Fox & Friends” to praise the president’s agenda. And he has appeared before reporters at the podium in the Pentagon press room only a handful of times, giving remarkably few on-the-record one-on-one news media interviews — one of which was with a reporter for a high school newspaper in Washington State who had obtained Mr. Mattis’s cellphone number.
“Secretary Mattis lives by a code that is part of his DNA,” said Capt. Jeff Davis, who retired last month from the Navy after serving as a spokesman for Mr. Mattis since early in the Trump administration. “He is genetically incapable of lying, and genetically incapable of disloyalty.”
That means the defense secretary’s only recourse is to stay silent, aides to Mr. Mattis said. He neither wants to publicly disagree with his boss nor is comfortable with showering false praise on Mr. Trump.
But cracks are showing.
In April, John R. Bolton became the White House national security adviser, replacing Army Lt. Gen. H. R. McMaster, who was long viewed as a subordinate to Mr. Mattis because of his rank as a three-star general compared with the retired Marine general’s four stars. Mr. Bolton is far more hawkish than either Mr. Mattis or General McMaster; administration officials said his deputy, Ms. Ricardel, actively dislikes the Pentagon chief — a feeling Mr. Mattis is believed to return in full.
Ms. Ricardel, a former Boeing executive who worked at the Pentagon during the George W. Bush administration, has a reputation for being as combative as Mr. Bolton.
As the Trump transition official responsible for Pentagon appointments, Ms. Ricardel stopped Mr. Mattis from hiring Anne Patterson as under secretary of defense for policy, one of the department’s highest political jobs. Ms. Patterson was a career diplomat who served as an ambassador under Presidents Bush, Barack Obama and Bill Clinton, but administration officials said Ms. Ricardel suspected Mr. Mattis was trying to load up the Pentagon with Democrats and former supporters of Hillary Clinton’s 2016 presidential campaign.
(Mr. Mattis also tried, unsuccessfully, to hire Michèle A. Flournoy, a Defense Department under secretary in the Obama administration, as his deputy. “He needed a deputy who wouldn’t be struggling every other day about whether they could be part of some of the policies that were likely to take shape,” Ms. Flournoy told a conference hosted by Politico.)
After a stint at the Commerce Department, Ms. Ricardel moved to the White House as Mr. Bolton’s deputy. Since her arrival, friction has increased between the White House and the Pentagon — along with speculation from West Wing aides that Mr. Mattis’s star is falling.
For instance, Mr. Mattis has recently resisted White House attempts to closely supervise military operations by demanding details about American troops involved in specific raids in Afghanistan and elsewhere.
One American official said the White House had bypassed the Pentagon by getting classified briefings of upcoming operations directly from the Special Operations task forces, to the frustration of Mr. Mattis.
That may seem a small and insular example of bureaucratic gamesmanship. But administration officials said it illustrates the tensions between Mr. Mattis and Mr. Trump: Either the defense secretary cannot appeal to the president, or he has and Mr. Trump is refusing to back him up.
Asked about disagreements between the National Security Council and the Pentagon, Garrett Marquis, a council spokesman, said in an email that “Ambassador Bolton is coordinating and working closely with all national security agencies to provide the president with national security options and guidance.”
In contrast with General McMaster, Mr. Bolton recently began attending regular weekly meetings between Mr. Mattis and Mr. Pompeo. Pentagon officials complain that White House interference has returned to the level of Susan E. Rice, who as Mr. Obama’s national security adviser was accused of micromanaging the department’s every move.
Mr. Mattis has repeatedly been blindsided by his boss this summer.
In June, Mr. Trump ordered Mr. Mattis to set up a Space Force over the defense secretary’s objections that such a move would weigh down an already cumbersome bureaucracy.
In July, the president blew up a NATO summit meeting that Mr. Mattis and other national security officials had worked on for months. The Pentagon chief and others saved the final agreement only because they shielded it from the president and urged envoys to complete it before Mr. Trump arrived in Brussels.
In August, the president undercut Mr. Mattis after a news conference at the Pentagon in which the defense secretary suggested that the United States military would resume war games on the Korean Peninsula. The exercises had been suspended — against Mr. Mattis’s advice — after Mr. Trump met with the North Korean leader, Kim Jong-un, in Singapore. “There is no reason at this time to be spending large amounts of money on joint U.S.-South Korea war games,” the president tweeted.
Meanwhile, Mr. Mattis has begun questioning the efficacy of Mr. Trump’s decision to pull out of the Iran nuclear deal — a move that, again, was made against his advice. Mr. Mattis has told aides that he has yet to see any difference in Iran’s behavior since Mr. Trump withdrew the United States from the agreement between world powers and Tehran.
Mr. Mattis famously was pushed out of his job as head of United States Central Command in 2013 because he was viewed as too much of a hawk on Iran policy during the Obama administration. But now, in the Trump administration, Mr. Mattis makes his arguments on Iran from the left of Mr. Bolton, Ms. Ricardel and the president himself.
For Mr. Trump, getting rid of his popular defense secretary would carry a political cost. Mr. Mattis is revered by the men and women of the American military. Most of the rest of his fans are people Mr. Trump does not care about: Democrats, establishment Republicans and American allies.
But moderate Republicans — whom Mr. Trump will need in 2020 — appear to trust Mr. Mattis as well, and firing him could hurt the president with that key group.
Mr. Trump, at the moment, is publicly standing by his defense secretary. “He’ll stay right there,,” the president told reporters last week when asked about Mr. Mattis’s comments in Mr. Woodward’s book. “We’re very happy with him. We’re having victories people don’t even know about.”
As for Mr. Mattis, “there’s no daylight between the secretary and the president when it comes to the unwavering support of our military,” said Dana W. White, the Pentagon press secretary. “It’s up to the president of the United States to decide what he wants to do.”
Eric Schmitt, Mark Landler and Julian Barnes contributed reporting.