The phone calls between President Trump and Sean Hannity come early in the morning or late at night, after the Fox News host goes off the air. They discuss ideas for Hannity’s show, Trump’s frustration with the ongoing special counsel probe and even, at times, what the president should tweet, according to people familiar with the conversations. When he’s off the phone, Trump is known to cite Hannity when he talks with White House advisers.

The revelation this week that the two men share an attorney is just the latest sign of how Hannity is intertwined with Trump’s world — an increasingly powerful confidant who offers the ­media-driven president a sympathetic ear and shared grievances. The conservative commentator is so close to Trump that some White House aides have dubbed him the unofficial chief of staff.

This portrait of the interactions between the president and the talk-show host is based on interviews with more than a dozen friends, advisers and associates of the two men, some of whom spoke on the condition of anonymity to describe private conversations.

For a president who feels, intensely, that he is under siege, Hannity offers what he prizes: loyalty and a mass audience. And Trump, in turn, has directed his supporters to Hannity’s show — urging people on Twitter last week to watch the commentator attack special counsel Robert S. Mueller III, who heads the investigation into Russian interference in the 2016 campaign.

Their bond intensified during the 2016 campaign and has grown stronger during Trump’s time in office.

“The bottom line is, during the heat of the campaign when relationships are forged, he was always there, offering good advice, in person and on television,” former deputy Trump campaign manager David Bossie said of Hannity. “The president sees him as an incredibly smart and articulate spokesman for the agenda.”

Trump and Hannity usually speak several times a week, according to people familiar with their relationship. The Fox News host, whose show averages more than 3 million viewers daily, is one of the few people who gets patched immediately to Trump. The two men review news stories and aspects of Hannity’s show, and occasionally debate specifics about whatever the president is considering typing out on Twitter. There have also been times when Trump has assessed the merits of various White House aides with Hannity.

The frequency of Hannity’s contact with Trump means that “he basically has a desk in the place,” one presidential adviser said.

Hannity and White House officials did not respond to requests for comment.

Several West Wing aides and friends of the president pointed to their running conversations — whether they take place over the phone or on the golf course in Florida, as they did in late March — as crucial to understanding this moment in the Trump presidency, when the president is eager to return to the combative and television-infused style of his business career and more isolated than ever from the traditional Republicans who have struggled to guide him.

“There is a small group of people who Trump speaks with who truly don’t have to be obsequious,” a veteran Trump ally said. “Sean is one of them,” the ally added, and said that Blackstone chief executive Stephen Schwarzman, veteran investor Carl Icahn and first lady Melania Trump “may be the only others on that list.”

Hannity’s counsel hews to a core theme — distance yourself from Washington elites and trust the instincts that he argues won Trump the White House — the advisers said, and Hannity has emphasized that keeping conservatives happy on immigration and health-care issues is critical.

Another regular topic: venting about the Russia probe and senior Justice Department figures such as Attorney General Jeff Sessions and Deputy Attorney General Rod J. Rosenstein, who oversees the investigation, the advisers said.

On air, Hannity has accused Rosenstein of launching a “war” on Trump and called Mueller part of a “deep state crime” family.

“Sometimes, Hannity gets him fired up,” the adviser said. “But Hannity also reminds him of what his base thinks.”

Hannity’s journey into Trump’s political sanctum took a decade to develop. The two became friendly before Trump became president, when Hannity would occasionally venture to Trump Tower to interview him, according to a Hannity producer who traveled with him.

“He rated very high,” this person said of Trump’s appearances on the show.

Trump’s personal attorney, Michael Cohen, was also a guest on Hannity’s program.

During one of Cohen’s appearances in 2012, Hannity noted that he was wearing a Trump tie.

“Loving it,” Cohen responded.

On Monday, an attorney for Cohen, who is under criminal investigation, revealed in federal court that the longtime Trump attorney had also done legal work for Hannity.

Hannity told viewers that night that the advice he sought from Cohen — which he did not disclose in recent weeks as he criticized raids of the lawyer’s office and residences — was “minor” and focused on real estate.

Fox News said Tuesday in a statement that while the network “was unaware of Sean Hannity’s informal relationship with Michael Cohen and was surprised by the announcement in court yesterday, we have reviewed the matter and spoken to Sean and he continues to have our full support.”

Advisers, at times, refer to Hannity as the “shadow” chief of staff, rivaling White House chief of staff John F. Kelly in terms of influence. Whenever Trump is irritated by his staff, he turns to outside allies, and Hannity is usually atop the call list.

Hannity’s relationship with attorney Jay Sekulow played a part in Sekulow signing on to Trump’s legal team on the Russia investigation, they said, adding that Hannity’s work with lawyers Victoria Toensing and her husband, Joseph diGenova, also contributed to the pair being considered to come aboard, although they did not ultimately do so.

Hannity long urged Bill Shine — who was Fox News’s co-president and had been Hannity’s first producer at Fox News but resigned last year — to join the Trump administration and has spoken highly of Shine to White House advisers. Shine, however, has declined to engage in serious talks about a job, an official said.

Hannity’s relationships with Trump’s sons, Donald Trump Jr. and Eric Trump, have served as the “glue” of his relationship with the president, one White House official said.

The two men appreciate Hannity bringing them on his program and recognize how those appearances have been important for their own profiles, the official said, noting that Hannity was a constant presence with the family at debates during the presidential campaign and widely liked by Trump family members.

And Hannity’s programs, whether on radio or television, have become a trusted gathering place for Trump allies during the campaign and after, providing a chronicle of sorts of the views of the president’s camp and the issues that have animated them as they have adjusted to power and faced challenges.

Cohen appeared on Hannity’s TV show three days before Trump’s inauguration last year. Hannity pressed him on one issue: Given that Cohen was Trump’s personal attorney, “Can I assume that in that role, not being a government role, that you’d have attorney-client privilege with President Trump?”

“Yes, of course,” Cohen replied. “That relationship, hopefully, will last, you know, for — not four years, but eight years. I think he’s a wonderful man.”