“Nations with allies thrive, and those without allies decline — it’s that simple,” Mr. Mattis told airmen assembled before him at a conference outside Washington on Wednesday. “We must be willing to do more than to listen to our allies. We must be willing to be persuaded by them.”

He added, pointedly, “Not all the good ideas come from the nation with the most aircraft carriers.”

All of this has earned him not so much as a stray tweet from the president.

The president and the defense secretary are “absolutely in sync with how they view the importance of having strong allies in the world,” Dana W. White, the chief Pentagon spokeswoman, said, objecting to the characterization that the two were at odds in their public remarks this week.

Mr. Mattis is banking that his “Mad Dog” stock with Mr. Trump will remain high enough, long enough for him to accomplish his goals, current and former Pentagon officials say, which include containing North Korea, defeating the Islamic State, pressing for budget increases to lift flagging readiness and updating the military’s nuclear and ballistic-missile defense policies.

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.