John Walker Lindh, the suburban Californian who converted to Islam and later volunteered his services to an enemy army in Afghanistan, pleaded guilty yesterday in federal court in Alexandria to fighting for the Taliban in a deal that will send him to prison for up to 20 years.

The surprise plea was negotiated over the weekend and concluded early yesterday. It was announced before a packed courtroom awaiting the start of what was to be a crucial evidentiary hearing on whether statements Lindh made while in custody in Afghanistan could be used against him at his trial, which had been set to start next month.

The deal spares Lindh the risk of a life sentence. In return, the government does not have to chance the release of sensitive information at trial or subject a host of intelligence and military officials to cross-examination in open court.

Lindh's admission brought an abrupt halt to a case that had promised an inside look at the daily ordeal of war. The 21-year-old's odyssey from Marin County, Calif., student to Muslim mercenary intrigued the nation and prompted his attorneys to liken him to other traveling American soldiers, including Ernest Hemingway and John Dos Passos -- an assertion federal prosecutors ridiculed.

Yesterday's hearing began with U.S. District Judge T.S. Ellis III noting the black curtains that had been installed to protect the identity of some witnesses -- even though the lawyers had given him a copy of the plea agreement 45 minutes earlier. Lindh's lead attorney, James J. Brosnahan, then rose to announce that there would be a change in plea.

Dressed in an Alexandria city jail-issued dark-green jumpsuit, Lindh stood and spoke publicly for the first time since pleading not guilty in February. Facing the judge, the former Takoma Park resident responded clearly and calmly as Ellis asked the questions asked of all defendants entering a guilty plea: their age, their background, whether they are knowingly and voluntarily pleading guilty.

When the judge asked Lindh what he was guilty of, Lindh answered carefully: "I provided my services as a soldier to the Taliban last year, from about August to November. In the course of doing so, I carried a rifle and two grenades. And I did so knowingly and willingly, knowing that it was illegal."

Assistant U.S. Attorney Randy I. Bellows then filled in some factual gaps. He said that Lindh reported to an Afghan recruiting office in Kabul in May of last year and told them that he was an American and that he wanted to go to the front line to fight. Last fall, after being trained in the use of explosives and battlefield tactics, he was sent to the trenches, carrying an AKM rifle and two grenades.

Although Lindh never fired on Americans, Bellows said he committed a felony by fighting for the Taliban, whose "control of Afghanistan provided protection and sanctuary for al Qaeda." Aiding the Taliban was outlawed by executive order in June 1999.

Of the 10 charges in the indictment filed against him -- including conspiracy to murder Americans abroad and assisting terrorists -- federal prosecutors agreed to drop nine. Lindh pleaded guilty to the remaining charge -- willfully supplying services to the Taliban -- as well as to a new count: carrying an explosive device while committing a felony.

Both sides agreed that Lindh would receive 10-year terms on each count, to be served consecutively. Ellis, who is not bound by the agreement, will formally impose sentence Oct. 4.

Lindh also agreed to cooperate with investigators by providing any information about al Qaeda and other groups in southern Asia and assisting other prosecutions. "I think his bottom-up perspective can be helpful," said Phillip Carter, a legal intern in the Pentagon's general counsel office. "Even a basic organization chart -- we don't have that."

Carter said that Lindh could be helpful in identifying the roles played by prisoners at the U.S. naval base at Guantanamo Bay, Cuba. "There's a certain level of frustration at Guantanamo. We're just grasping at straws," he said.

Both sides were pleased with the deal. "This was a good opportunity for the government to get a very good result," U.S. Attorney Paul J. McNulty said. He added that although he had no doubts about the strength of the government's case, a 20-year sentence "sends a very strong message to anyone who would be at all tempted to turn their backs on the United States."

Attorney General John D. Ashcroft called the plea "an important victory in America's war on terrorism."

Lindh's attorneys and family focused on the dropping of the most serious charges. "Our goal, frankly, was to try to give him some kind of future in the chaos of this case," Brosnahan said.

He said the defense was motivated to seek a plea agreement Friday, when he and Ellis clashed in court over the use of videotaped evidence for this week's hearing, an exchange for which the judge apologized yesterday. Earlier, Ellis had dismissed a slew of defense motions to eliminate charges or move the trial.

"The judge on Friday made it quite clear he probably was not going to grant any of the motions" to suppress Lindh's statements, Brosnahan said.

So the two sides began earnest negotiations over the weekend, with Lindh signing the agreement about 1:30 a.m. yesterday. Brosnahan said he first mentioned a plea possibility about six weeks ago, and sources said Justice Department lawyers presented the White House counsel's office with the parameters of a deal Wednesday. On Thursday, White House Counsel Alberto R. Gonzalez briefed President Bush and got his approval.

Shortly after 9 a.m. Sunday, both sides met at the U.S. attorney's office in the Alexandria courthouse. Lindh's attorneys, normally based in San Francisco, later went to their temporary office nearby, and faxes and phone calls began shuttling back and forth between the two sides, with Brosnahan twice going to the Alexandria jail to update Lindh.

When it became clear that Lindh might be freed from prison someday, he raised an important issue: He wanted the ability to travel so that he could make his hajj, or pilgrimage to Mecca, required of all Muslims at least once during their lifetime. Brosnahan said he would check, and prosecutors agreed to a travel clause.

Finally, at midnight, both sides reached a deal. Brosnahan said he arranged for another visit to Lindh and presented the plea agreement to him. "I think his reaction was relief," Brosnahan said. "I think he was relieved that we had been able to give him something, give him a future."

Where once he had sworn jihad with Osama bin Laden, now "he has no present jihad," Brosnahan said. "He has no allegiances to any of these groups."

Experts saw no clear-cut victor. "I think it's a good result for both sides," said Scott L. Silliman, a longtime Air Force lawyer who is now a law professor at Duke University. "A plea agreement is a great deal for the government, because I thought they would have a hard time proving Lindh conspired to kill a U.S. national overseas, and there would be some slight embarrassment if they lost on their main charge. And he's only pleading to specific charges to which he's always acknowledged."

From a chronology apparently provided by Lindh, prosecutors have alleged that the he crossed into Afghanistan in May or June 2001. In July or August, Lindh was issued a rifle and sent to the front line in northeastern Afghanistan. By early November, his unit, in steady retreat, had surrendered to Northern Alliance forces.

He was questioned at a prison in Mazar-e Sharif by CIA officer Johnny "Mike" Spann, but Lindh feared being seen as cooperating with the Americans and said nothing. That same day, prisoners launched a brutal uprising, during which Spann was fatally shot and Lindh was shot in the leg.

Spann, of Manassas Park, was the first American killed in Afghanistan, and some members of his family were not pleased with yesterday's deal. Spann's mother, Gail, told the Associated Press that the agreement was not fair to her family. "I'm sure it is to John Walker's family," she said, "but we don't think it is to us, of course. As Mike's mom, I would like for Mike to have had 20 years to live."

After the uprising," Lindh and other wounded prisoners were taken to a hospital at Shebergan. He also spoke with Army investigators, a freelance journalist working for CNN and an FBI agent.

In great detail, Lindh spoke of his time with the Taliban and al Qaeda forces, and his statements formed a key part of the government's case. His attorneys argued that the statements should be excluded from trial because he was wounded and weary, held naked and blindfolded in a steel box, not advised of his rights and not allowed to see a lawyer.

Lindh agreed to withdraw his claim of intentional mistreatment by U.S. forces -- a move that was important to the Pentagon, a government official said. "It removes the offense to their honor. The defense could have made it ugly, and they could have called a whole bunch of servicemen to the stand."

Although there is no parole in the federal system, credits for good behavior can reduce a sentence by up to 15 percent, meaning Lindh could be released in 17 years.

Staff writers Susan Schmidt, Thomas E. Ricks and Dana Milbank contributed to this report.

Outside the federal courthouse in Alexandria after news of the plea deal, John Walker Lindh's father, Frank, comforts daughter Naomi. Behind them is John Walker Lindh's brother, Connell.John Walker Lindh faces a sentence of up to 20 years."Our goal, frankly, was to try to give him some kind of future in the chaos of this case," said James J. Brosnahan, John Walker Lindh's lead attorney.