Robert P. Hanssen, gaunt and soft-spoken, was sentenced to life in prison yesterday as he apologized for two decades of spying for Moscow that caused nearly unparalleled damage to the United States in one of the FBI's most embarrassing chapters.

Hanssen's fate had already been settled under a July 2001 plea agreement that spared him the death penalty in exchange for his cooperation. But the seven-minute hearing in a federal courtroom in Alexandria marked the first time the veteran FBI counterintelligence agent has spoken a full sentence in public since his arrest.

Hanssen, 58, an agent for 25 years, twisted his hands behind his back and scanned the courtroom for familiar faces before stepping to the microphone and reading a carefully written statement:

"I apologize for my behavior. I am shamed by it. I've betrayed the trust of so many. I opened the door for calumny against my totally innocent wife and children. I've hurt them deeply. I've hurt so many deeply."

His wife, Bonnie, and six children did not attend, but several of Hanssen's friends came, and dozens of FBI agents packed the courtroom to bear witness to the fate of the former colleague who betrayed them.

U.S. District Chief Judge Claude M. Hilton did not comment on the case as he handed down the life sentence, which does not allow for the possibility of parole. Hanssen has asked to serve the time in the federal prison in Allenwood, Pa., which houses other prominent spies, including former CIA officer Aldrich H. Ames, who betrayed some of the same Soviet double agents as Hanssen. Two of the Soviet spies were executed by their country.

"Their blood is on [Hanssen's] hands," Assistant U.S. Attorney Randy I. Bellows said. Hanssen "broke every promise he ever made to the FBI, the American government and the American people. . . . He was the worst kind of thief . . . a traitor in our midst with no line he would not cross."

Hanssen, who began spying in 1979, was arrested in February 2001 shortly after he was videotaped leaving a package of classified documents in a Fairfax County park for his Russian handlers. He pleaded guilty to receiving $643,000 in cash and diamonds and $800,000 in a Russian bank.

The case has captured the public's imagination because Hanssen's image as a faithful Roman Catholic, a devoted family man and a hard-edged conservative agent contrasted so sharply with his espionage and a series of sexual secrets that have come to light.

Although Hanssen spent much of the money he received from the Russians on home improvements and school tuition, he also lavished gifts, including a car, on a local stripper. Hanssen also posted explicit stories about his wife on the Internet.

Yet, when Bonnie Hanssen discovered her husband's spying in the early 1980s, he agreed to stop, consult a priest and give some of his early profits to Mother Teresa's charities in India. He resumed spying in 1985.

In his brief statement, Hanssen made a point of recognizing the help he and his family have received since his arrest. "I wish to thank all my family, friends and co-workers and those who have provided support for my family and friends," he said. "I am humbled by your generosity."

The spying case has also provoked recriminations at the FBI, which began routinely polygraphing agents after Hanssen's arrest. A commission headed by former FBI director William Webster recently detailed a series of investigative failures that allowed Hanssen to escape detection for so long.

Considered one of the most damaging spies in U.S. history, he turned over 6,000 pages of highly classified documents and provided details about U.S. preparations for nuclear war, several top-secret communications programs and a listening tunnel beneath the Soviet Embassy in Washington. Trained in counterintelligence, Hanssen was able to evade capture for decades until a defector warned the FBI of a high-level traitor and provided examples of the highly classified information that had been compromised.

Hanssen attorney Plato Cacheris, who also represented Ames and other spies, said Hanssen's "artfulness in conducting this activity is the best I have ever seen."

Cacheris declined to speculate on why his client turned to treachery. "There were monetary reasons," he said. "There were ego reasons. . . . None of them are valid, or we wouldn't be here today."

U.S. Attorney Paul J. McNulty said Hanssen's fate should serve as a warning to would-be spies. "Robert Hanssen was trained to catch spies," McNulty said. "He was an expert in what it took to avoid being caught, and he was caught and he was punished. That is what will happen to anyone who betrays our country."

Since his guilty plea in July, Hanssen has been polygraphed twice and interviewed 75 times by the Webster Commission and three other government agencies trying to assess his activities and the damage he caused. The Justice Department inspector general and a CIA task force have expressed concern about the level of cooperation Hanssen provided, but the FBI and the Webster Commission said they were satisfied.

McNulty said prosecutors concluded that the government had no reason to break the plea agreement and seek the death penalty. "We think the extent of the debriefings is significant," he said. The government also told the court that Bonnie Hanssen has fully cooperated with the investigation and, as set out in federal law, will receive a government pension of just under $40,000.

Van Harp, who heads the Washington Field Office of the FBI, which investigated Hanssen, said: "Justice was served. This was closure to the darkest chapter in the history of the FBI."

The hearing drew flocks of journalists, counterintelligence agents and some very interested observers -- among them, actor Ron Silver, who will play Hanssen's former boss, David Major, in a movie about the spy.

"This is [Hanssen's] last public appearance," Silver said. "To see him scour the audience for somebody, that's a very interesting thing for somebody like me."

For his part, Major, who supervised Hanssen's unit from 1988 to 1990, said: "I was very glad Bob said what he did. I think it was heartfelt. . . . He's come to terms with what he has done, despicable as it is. . . . Now he has to spend every waking moment in jail. His only escape is in sleep and death. That's really sad but appropriate.

"What got me is, he knew better."

U.S. Attorney Paul J. McNulty is surrounded by other attorneys and reporters after the sentencing of Robert P. Hanssen. McNulty said the case should serve as a warning to would-be spies: "He was caught and he was punished."In his apology, Hanssen said he had "betrayed the trust of so many."