Armed guards stand at the gates. IDs are needed to pass through electronic barriers. And uniformed members of the American military – trained and battle-tested to recognize the enemy and kill – are everywhere, smartly saluting as they come and go.

And yet, twice in less than four years, a person with permission to be there passed through the layers of protection at a U.S. base and opened fire, undermining the sense of security at the installations that embody the most powerful military in the world.

“It is earth-shattering. When military bases are no longer safe, where is safe if that even doesn’t exist anymore?” said Col. Kathy Platoni, a reservist who keeps a gun under her desk after witnessing the shooting at Fort Hood in Texas in 2009, when Army psychiatrist Maj. Nidal Hasan killed 13 people.

In the wake of this week’s deadly rampage at the Washington Navy Yard, Defense Secretary Chuck Hagel ordered the Pentagon to review security at all U.S. defense installations worldwide and the issuing of security clearances that allow access to them.

“We will find those gaps and we will fix those gaps,” Hagel vowed on Wednesday.

After Fort Hood, the military tightened security at bases nationwide. Those measures included issuing security personnel long-barreled weapons, adding an insider-attack scenario to their training, and strengthening ties to local law enforcement, said Peter Daly, a vice admiral who retired from the Navy in 2011. The military also joined an FBI intelligence-sharing program aimed at identifying terror threats.

Then, on Monday, Aaron Alexis, a 34-year-old former Navy reservist who held a security clearance as an information technology employee at a defense company, used a valid pass to get into the Washington Navy Yard and killed 12 people before dying in a gun battle with police.

Among other things, the attack has raised questions about the adequacy of the background checks done on government contractors who hold security clearances. Alexis had a history of violent behavior and was said to be hearing voices.

Many of the security improvements adopted after 9/11 and Fort Hood were created largely with terrorism in mind, not unstable individuals with no apparent political agenda. Those threats can be more difficult to detect.

Daly, who directs the U.S. Naval Institute in Annapolis, Md., said more can be done when it comes to vetting people before issuing them credentials that allow them inside.

“The bottom line is, once you’re inside that hardened line of defense, that is the most difficult scenario,” he said. “We need to look at how these clearances are granted to contractors and subcontractors and to make sure once someone is granted clearance, that we come back and check again.”

Some of the shock and sudden sense of vulnerability caused by Fort Hood and the Navy Yard attack may be based on the mistaken belief that military personnel are armed when they are on domestic installations.

Most military personnel are, in fact, barred from carrying weapons onto a base, and Hasan and Alexis probably knew it. Another little-known fact is that many searches are random. Not all vehicles or packages are checked.

John Barney, the owner of Tri-Star Commercial, an Austin-based security company that has installed cameras and card access systems at several military installations, said that after Fort Hood, the military mostly responded by increasing the armed police presence, but added few electronic measures.

But he admitted electronic security is not necessarily enough.

Just recently, he said, he was in a military warehouse area near San Antonio and entered without showing any identification or encountering military police. In one building, he came across an open metal cage stacked with M-16 rifles that anyone could have walked off with, he added.

“I’ve noticed gaps and holes that make me concerned,” Barney said.

Joshua Liam, a sailor at the Pensacola Naval Air Station in Florida, said the base there is vulnerable because it is open to the public.

“There is not a lot that can be done other than strengthening the first line of defense at the gates,” he said. “We are taking security precautions on the base, but I don’t know if it could stop someone who really wanted to cause harm.”

In the San Diego area, Karen Archipley, the wife of a retired Marine, visits Marine bases regularly for her work with an organic farm that trains veterans in agriculture. She said her sense of security has not been shaken.

“It’s now happened twice in the past five years, but I would tell you that those are individual incidents. It wouldn’t matter where those people were. They could have been at a post office,” she said. “If somebody is unstable, unsteady, it doesn’t matter. I don’t feel like on base it’s dangerous.”

WASHINGTON (AP) — The former Navy reservist who slaughtered 12 people at the Washington Navy Yard had been hearing voices and was being treated for mental problems in the weeks before the shooting rampage, but was not stripped of his security clearance, officials said Tuesday.

Aaron Alexis, a 34-year-old information technology employee with a defense contractor, used a valid pass to get into the highly secured installation Monday morning and started firing inside a building, the FBI said. He was killed in a gun battle with police.

The motive for the mass shooting – the deadliest on a military installation in the U.S. since the attack at Fort Hood, Texas, in 2009 – was a mystery, investigators said.

U.S. law enforcement officials told The Associated Press that there was no known connection to international or domestic terrorism

and that investigators have found no manifesto or other writings suggesting a political or religious motivation.

Alexis had been suffering a host of serious mental problems, including paranoia and a sleep disorder, and had been hearing voices in his head, according to the officials, who spoke on condition of anonymity because the criminal investigation was still going on.

He had been treated since August by Veterans Affairs for his mental problems, the officials said.

The Navy had not declared him mentally unfit, which would have rescinded a security clearance Alexis had from his earlier time in the Navy Reserves.

The assault is likely to raise more questions about the adequacy of the background checks done on contract employees and others who are issued security clearances – an issue that came up most recently with National Security Agency leaker Edward Snowden, an IT employee with a government contractor.

In the hours after the Navy Yard attack, a profile of Alexis began coming into focus.

A Buddhist convert who had also had flare-ups of rage, Alexis, a black man who grew up in New York City and whose last known address was in Fort Worth, Texas, complained about the Navy and being a victim of discrimination. He also had run-ins with the law over shootings in 2004 and 2010 in Texas and Seattle, and was ticketed for disorderly conduct after being thrown out of a metro Atlanta nightclub in 2008.

Alexis’ bouts of insubordination, disorderly conduct and being absent from work without authorization prompted the Navy to grant him an early – but honorable – discharge in 2011 after nearly four years as a full-time reservist, authorities said. During his service, he repaired aircraft electrical systems at Fort Worth.

In addition to those killed at the Navy Yard attack, eight people were hurt, including three who were shot and wounded, authorities. Those three were a police officer and two female civilians, authorities said. They were all expected to survive.

Monday’s onslaught at a single building at the Navy Yard unfolded about 8:20 a.m. in the heart of the nation’s capital, less than four miles from the White House and two miles from the Capitol. It put all of Washington on edge.

“This is a horrific tragedy,” Mayor Vincent Gray said.

Alexis carried three weapons: an AR-15 assault rifle, a shotgun, and a handgun that he took from a police officer at the scene, according to two federal law enforcement officials who spoke on condition of anonymity because they were not authorized to discuss the investigation.

The AR-15 is the same type of rifle used in last year’s shooting at a Newtown, Conn., elementary school that killed 20 children and six adults. The weapon was also used in the shooting at a Colorado movie theater that killed 12 and wounded 70.

For much of the day Monday, authorities said they were looking for a possible second attacker who may have been disguised in an olive-drab military-style uniform. But by late Monday night, they said they were convinced the shooting was the work of a lone gunman, and the lockdown around the area was eased.

“We do now feel comfortable that we have the single and sole person responsible for the loss of life inside the base today,” Washington Police Chief Cathy Lanier said.

President Barack Obama lamented yet another mass shooting in the U.S. that he said took the lives of American “patriots.” He promised to make sure “whoever carried out this cowardly act is held responsible.”

The FBI took charge of the investigation.

The attack came four years after Army psychiatrist Maj. Nidal Hasan killed 13 people at Fort Hood in what he said was an effort to save the lives of Muslims overseas. He was convicted last month and sentenced to death.

The dead in the Navy Yard attack ranged in age from 46 to 73, according to the mayor. A number of the victims were civilian employees and contractors, rather than active-duty military personnel, the police chief said.

At the time of the rampage, Alexis was an employee with The Experts, a company that was a Defense Department subcontractor on a Navy-Marine Corps computer project, authorities said.

Valerie Parlave, head of the FBI’s field office in Washington, said Alexis had access to the Navy Yard as a defense contractor and used a valid pass.

The Washington Navy Yard is a sprawling, 41-acre labyrinth of buildings and streets protected by armed guards and metal detectors, and employees have to show their IDs at doors and gates. More than 18,000 people work there.

The rampage took place at Building 197, the headquarters for Naval Sea Systems Command, which buys, builds and maintains ships and submarines. About 3,000 people work at headquarters, many of them civilians.

Witnesses on Monday described a gunman opening fire from a fourth-floor overlook, aiming down on people on the main floor, which includes a glass-walled cafeteria. Others said a gunman fired at them in a third-floor hallway.

Patricia Ward, a logistics-management specialist, said she was in the cafeteria getting breakfast.

“It was three gunshots straight in a row – pop, pop, pop. Three seconds later, it was pop, pop, pop, pop, pop, so it was like about a total of seven gunshots, and we just started running,” Ward said.