In interviews, Muslim soldiers said they had all encountered at one time or another what one called “knucklehead” comments equating them with terrorists. Things got worse after 13 people were killed at Fort Hood in 2009 by a Muslim Army psychiatrist who said the United States’ wars in Iraq and Afghanistan were wars against all Muslims.

Other problems come from the cultural barriers, like a ban on facial hair, and dealing with military food that is often rife with pork, forbidden by Islam. Few bases have Muslim prayer services, and only five of the Army’s roughly 2,900 chaplains are imams.

“It can be challenging,” said Mr. Rashid, whose family moved from Pakistan when he was 10. “The nature of military service is not very conducive to practicing your faith, but Islam is flexible.”

“I am here as an American,” he added. “I benefit from the liberty and opportunity of this country, and it is my obligation to serve this nation in some way.”

Thousands of Muslims have served in the military since at least the Civil War, but they make up a disproportionately small portion of the force. Just 3,939 troops currently list their faith as Islam, according to Pentagon data. They make up just 0.3 percent of the military; Muslims are estimated to make up about 1 percent of the civilian population.