Mr. Mattis has stayed out of the chaos that has characterized the Trump administration in part because of his ability, friends and associates say, to present an agreeable demeanor. His calm, folksy air eases the sting of his remarks and keeps Mr. Trump from feeling threatened even when his defense secretary contradicts him or slow-walks his pet policies, according to five people close to the secretary, who, like others interviewed, would not be named describing the relationship between the two men.

He has taken pains not to criticize the president publicly, though he has disagreed over policy and personnel with the president and his senior staff — and Mr. Trump has reminded people around him that he regards Mr. Mattis, a retired four-star general, as “a Democrat” who is more liberal on many issues than he is.

Mr. Mattis won one early victory that established his authority internally. Shortly before inauguration, Mr. Mattis signaled he intended to pick Michèle A. Flournoy, a hawkish Democrat who would most likely have been Hillary Clinton’s defense secretary, as his chief deputy.