PENSACOLA, Fla. — A member of the Saudi Air Force armed with a handgun fatally shot three people and injured eight others on Friday morning during a bloody rampage in a classroom building at the prestigious Naval Air Station in Pensacola, Fla., where he was training to become a pilot.

The authorities, led by the F.B.I., were investigating to determine the gunman’s motive and whether the shooting was an act of terrorism.

A United States military official identified the suspect, who was killed by a sheriff’s deputy during the attack, as Second Lt. Mohammed Saeed Alshamrani. He was one of hundreds of military trainees at the base, which is considered the home of naval aviation.

Six other Saudi nationals were detained for questioning near the scene of the shooting, including three who were seen filming the entire incident, according to a person briefed on the initial stages of the investigation. A group that monitors online jihadist activity said that shortly before the shooting, a Twitter account with a name matching the gunman’s posted a “will” calling the United States a “nation of evil” and criticizing its support for Israel.