The first time I met Michael Flynn, whom President-elect Donald Trump tapped last week to be his national-security adviser, he was wearing the Army’s weekend uniform—a baggy polo shirt and khaki pants—and swinging his Blackberry around like a cowboy would his revolver. It was the late summer of 2008, at a Washington cocktail party hosted by Flynn’s boss, Admiral Michael Mullen, who was then the chairman of the Joint Chiefs of Staff. Flynn was Mullen’s top intelligence guy.

“Look at this!” Flynn said, holding up his phone so that I could see the screen. At his request, his communications staff would send him the daily dispatches published by tribal media outlets in Pakistan’s troublesome northwest region. These articles chronicled skirmishes, feuds, and revenge killings—it was unfiltered information that any decent Western news stringer would know how to read, but that, seven years into the war in Afghanistan, the American military was still far from absorbing. Flynn got it, though. He was drawn to the little flecks of truth scattered on the ground.

A lot of reporters and other civilians found Mike, as everyone called him, refreshing. A plucky Irish Catholic kid from Rhode Island, he wasn’t impressed by rank. He told his junior officers to challenge him in briefings. “You’d hear them say, ‘Boss, that’s nuts,’ ” one former colleague said. The colleague asked not to be named, as did others I talked to for this story, either because they wanted to maintain a positive relationship with Flynn or because they did not want to criticize the incoming Administration. “When he would walk in a room, they would look up like little dogs. They just loved him.”

Flynn broke rules he thought were stupid. He once told me about a period he spent assigned to a C.I.A. station in Iraq, when he would sometimes sneak out of the compound without the “insane” required approval from C.I.A. headquarters, in Langley, Virginia. He had technicians secretly install an Internet connection in his Pentagon office, even though it was forbidden. There was also the time he gave classified information to NATO allies without approval, an incident which prompted an investigation, and a warning from superiors. During his stint as Mullen’s intelligence chief, Flynn would often write “This is bullshit!” in the margins of classified papers he was obliged to pass on to his boss, someone who saw these papers told me.

Read more analysis of Donald Trump's Cabinet appointments.

The greatest accomplishment of Flynn’s military career was revolutionizing the way that the clandestine arm of the military, the Joint Special Operations Command (JSOC), undertook the killing and capture of suspected terrorists and insurgents in war zones. Stanley McChrystal, Flynn’s mentor, had tapped him for the job. They were both part of the self-described “Irish mafia” of officers at the Fort Bragg Army base, in North Carolina. In Afghanistan and Iraq, Flynn ordered JSOC commandos to collect and catalogue data from interrogations, captured electronic equipment, pocket trash—anything that could yield useful information. By analyzing these disparate scraps of intelligence, they were able to discover that Al Qaeda was not a hierarchical group after all but a dynamic network of cells and relationships. As I learned while doing research for my book “Top Secret America,” Flynn and McChrystal dramatically increased the pace of JSOC attacks on enemy hideouts by devising a system in which commandos on missions transferred promising data—cell-phone numbers, meeting locations—to analysts, who could then quickly point them to additional targets to hit. Multiple raids a night became common.

McChrystal, who was appointed to run JSOC in 2003, brought Flynn in as his intelligence chief to help him shake up the organization. Flynn was one of the few high-ranking officers who disdained the Army’s culture of conformity. But McChrystal also knew he had to protect Flynn from that same culture. He “boxed him in,” someone who had worked with both men told me last week, by encouraging Flynn to keep his outbursts in check and surrounding him with subordinates who would challenge the unsubstantiated theories he tended to indulge.

In mid-2007, Flynn returned home with three years of JSOC secrets in his head. He had witnessed close-quarters combat and killings. He had helped load the bodies of dead and wounded Seal Team 6 and Delta Force warriors into evacuation helicopters. Like his comrades, he had spent twenty hours a day, seven days a week, focussed on killing the enemy. Sometimes women and children were killed, too. He wouldn’t even take a break to attend his son’s wedding, a moment of personal sacrifice he mentions often when reflecting on those days.

In 2012, Flynn became director of the Defense Intelligence Agency, in charge of all military attachés and defense-intelligence collection around the world. He ran into serious trouble almost immediately. I’ve spoken with some two dozen former colleagues who were close to Flynn then, members of the D.I.A. and the military, and some who worked with him in civilian roles. They all like Flynn personally. But they described how he lurched from one priority to another and had trouble building a loyal team. “He made a lot of changes,” one close observer of Flynn’s time at the D.I.A. told me. “Not in a strategic way—A to Z—but back and forth.”

Flynn also began to seek the Washington spotlight. But, without loyal junior officers at his side to vet his facts, he found even more trouble. His subordinates started a list of what they called “Flynn facts,” things he would say that weren’t true, like when he asserted that three-quarters of all new cell phones were bought by Africans or, later, that Iran had killed more Americans than Al Qaeda. In private, his staff tried to dissuade him from repeating these lines.

Flynn’s temper also flared. He berated people in front of colleagues. Soon, according to former associates, a parallel power structure developed within the D.I.A. to fence him in, and to keep the nearly seventeen-thousand-person agency working. “He created massive antibodies in the building,” the former colleague said.

Flynn had been on the job just eighteen months when James Clapper told him he had to go. Clapper said that he could stay for another nine months, until his successor was vetted and confirmed, according to two people familiar with their conversation. Flynn was livid.

After he left government, Flynn followed the path of many other retired generals and got on the television and speaking circuit. He wrote a book with Michael Ledeen, a controversial neoconservative foreign-policy analyst, about defeating terrorism. Islam is not a religion, Flynn and Ledeen wrote, but a political ideology bent on destroying Judeo-Christian civilization. Flynn began saying that he had been fired because President Obama disagreed with his views on terrorism and wanted to hide the growth of ISIS. I haven’t found anyone yet who heard him say this while he was still in the military. In the past, I’ve asked Flynn directly about this claim; he has told me that he doesn’t have any proof—it’s just something he feels was true. (Flynn did not respond to requests for comment for this article.)

As Flynn’s public comments became more and more shrill, McChrystal, Mullen, and others called Flynn to urge him to “tone it down,” a person familiar with each attempt told me. But Flynn had found a new boss, Trump, who enlisted him in the fight against the Republican and Democratic Party establishments. Flynn was ready. At the Republican National Convention, Flynn boiled over in front of an audience of millions. He led the crowd in chants of “Lock her up! Lock her up!” His former colleagues say they were shocked by what they saw.