Their arrests in June — along with the arrest a month earlier of the Times Square car bomb suspect, Faisal Shahzad — underscored the concern over terrorists born or raised in the United States. Mr. Shahzad worked and owned a home in Connecticut; and the New Jersey suspects were products of communities not far from Manhattan: Mr. Alessa lived in North Bergen, Mr. Almonte in Elmwood Park.

There was no evidence in the public record that Mr. Almonte and Mr. Alessa had established contact with the Shabab, but officials said the guilty pleas were a reminder of the importance of sustained vigilance to prevent terrorist attacks.

“One of the goals of organizations like Al Shabab and Al Qaeda is to influence people both here and abroad to get them to join the fight,” Paul J. Fishman, the United States attorney for New Jersey, said after the hearing. “We are very, very focused in New Jersey and around the country on combating that threat.”

The two men appeared together before Judge Dickinson R. Debevoise in United States District Court in Newark, wearing long beards and blue smocks. They pleaded guilty to conspiracy to murder people outside the United States “whose beliefs and practices did not accord with their extremist ideology,” court papers said.

The maximum sentence for the charge is life in prison, but defense lawyers and prosecutors agreed that neither side would contest sentences in the range of 15 to 30 years.

The Shabab are known to have recruited more than 20 Americans over the past several years. Many were young Somali refugees from the Minneapolis area. But also among them is Omar Hammami, the son of a Syrian immigrant and an American, who appears in Shabab videos and is believed to occupy a leadership position in the group.

Last July, the Shabab claimed responsibility for the bombings that killed 70 people in Uganda as they gathered to watch the telecast of the World Cup final. The group is also believed to have provided shelter to Qaeda operatives involved in the 1998 bombings of the United States Embassies in Kenya and Tanzania.

Mr. Almonte and Mr. Alessa had been under surveillance by the Federal Bureau of Investigation since 2006, according to court papers. As the investigation progressed, an undercover officer from the New York Police Department, known as Bassem, befriended them and recorded conversations in which they talked about killing American troops and other “kufr,” or nonbelievers, overseas.

An error has occurred. Please try again later.

You are already subscribed to this email.

“My soul cannot rest until I shed blood,” Mr. Alessa said in one recording.

“I wanna, like, be the world’s known terrorist.”

In January 2010, Bassem recorded a discussion about the various armed groups in Somalia in which Mr. Almonte said the Shabab were “the main one.”

Since childhood, Mr. Alessa had been prone to angry outbursts, and his parents, who are Palestinian immigrants, struggled to control him. He first saw a psychiatrist at age 6 and changed schools 10 times, they said in interviews last summer.

Mr. Almonte, who went by the name Omar and came from the Dominican Republic as a child, was similarly alienated. As a teenager, he was arrested for carrying a knife to school, fighting and drinking. After finishing high school, he drifted toward an intolerant strain of Islam, putting him at odds with his family. Around the same time, he met Mr. Alessa.

By the time the men were arrested at the airport, the government had extensive information on their activities. As they admitted in court on Thursday, the men had been preparing themselves mentally and physically for combat. They worked out, went on hiking and paintball excursions and stocked up on night-vision goggles. They watched Shabab propaganda videos and listened to speeches by Anwar al-Awlaki, a radical cleric born in the United States whose messages have appealed to young, disaffected Muslims in the West.

In 2007, the two men traveled to Jordan to try to join extremist groups there, but they did not succeed. They discussed attacks against non-Muslims on American soil if they failed abroad once again.

Stanley L. Cohen, a lawyer for Mr. Alessa, said the case would have been “almost impossible” to win at trial because of a low legal threshold to prove conspiracy, and the difficulty of arguing terrorism cases.

“People have to recall, these are very young men,” he said. Referring to the role of the undercover officer, he added, “The judge will have to decide how much of this was feeding kids.”

Several relatives of Mr. Alessa watched the proceeding somberly and left court without commenting.

The men were returned to the Metropolitan Detention Center in Brooklyn, where they have been held since shortly after their arrests. Sentencing was tentatively scheduled for late June.

A version of this article appears in print on March 4, 2011, on Page A22 of the New York edition with the headline: New Jersey Men Who Tried to Join Terror Group Plead Guilty. Order Reprints|Today's Paper|Subscribe