Two mass shootings at U.S. military installations in one week, including one in which the perpetrator was a foreign national, have prompted questions over firearm use on American bases.

Friday's shooting by a Saudi national who was in the United States for navy training left three people dead and eight injured at Naval Air Station Pensacola in Florida. It came two days after a sailor shot two civilian Defense Department workers dead at Pearl Harbor Naval Shipyard in Hawaii before killing himself.

To have two shootings clustered one after another on military bases is unusual. In the past two decades, there have only been about seven other active shootings on bases; the deadliest was a November 2009 shooting spree in Fort Hood, Texas, which killed 13 and injured 32.

Gunfire on bases is rare partly because access to weapons is highly restricted, with military facilities generally forbidding anyone from carrying a firearm — even if it is government-issued.

Government-issued firearms are locked in an arms room on base and only distributed when they are needed for training, MSNBC military analyst and retired Army Col. Jack Jacobs said. Personal weapons are prohibited on base, but they must be registered with the base even though they are not to be used there.

This regulation has been violated in the past: In September 2013, Aaron Alexis, a contractor who had authorized access at the Navy Yard in Washington, D.C., took an unassembled shotgun onto the base in a bag. Once inside, he reassembled the weapon and opened fire, killing 12 people.

Military personnel know they could face "significant" risks to their careers if they carry a firearm onto a base in their vehicles, said Rick "Ozzie" Nelson, a former Navy helicopter pilot and terrorism expert for the Center for Strategic and International Studies, a nonprofit policy research organization.

While each base has slightly different security procedures, most typically provide only a cursory check of vehicles of authorized people when they enter, not anticipating there will be any weapons, whereas delivery trucks or visitors are given a thorough examination, Nelson said.

The firearm ban applies even in open-carry state, MSNBC national security analyst Clint Watts said, because military bases are federal property. He said the bases would struggle to operate if they did more thorough checks of everyone who has authorized access.

"There is no way you could search all the vehicles coming into these bases every day," Watts said. "Often times, you're talking about tens of thousands of people, depending on the size of the base, coming through. It just is completely infeasible."

The ban on firearms has been debated over the years, especially under President Donald Trump, who vowed in January 2016 that on his first day in office, he would "get rid of gun-free zones on schools, and on military bases.”

While the ban has remained, the Department of Defense took a step in November 2016 to loosen restrictions on privately owned guns, writing in a directive that base commanders could grant permission to personnel who asked to carry a privately owned firearm “for a personal protection purpose not related to performance of an official duty or status.” Not all base commanders are willing to make that exception, and it is considered on a very strict case-by-case basis, according to experts.

Social media lit up Friday with opinions about military bases being gun-free zones, with some like Democratic presidential candidate Elizabeth Warren arguing, "Not even our military bases are safe from gun violence. I'm heartsick for the victims and their families. We must end this epidemic."

Others, such as director and producer Robby Starbuck, tweeted, "My opinion is that no foreign national should have a weapon on a U.S. base unless our soldiers are armed."

The bases take other precautions to increase security, and active shooters are just one of a wide range of threats they prepare for. Everyone who has access to a base undergoes a routine background screening, with those with higher military clearance or government clearance subject to a higher level of screening that might include questions about mental health, Nelson said.

The bases also conduct active shooter drills for all employees, according to Nelson. They train closely with local police forces to practice responses to active shooters — something Naval Air Station Pensacola Capt. Timothy Kinsella Jr. credited as saving lives, since members of the Escambia County Sheriff's Office engaged in a gunfight to neutralize the shooter there.

"It could have been a lot worse if we didn't have the assistance of our local partners," he said at a press conference Friday.