Gunman Seifeddine Rezgui, who killed 38 tourists in a Tunisian beach attack, was trained in neighbouring Libya at the same time as the attackers who targeted the Bardo museum in March, an official has said.
Security chief Rafik Chelli said gunman Rezgui, a Master's student in electrical engineering at Kairouan University, sneaked into Libya in January and trained near the western town of Sabratha.
The two attackers who carried out the Bardo museum attack in Tunis in March that killed 22 people were there at the same time.
The attack near the resort town of Sousse was the worst in Tunisia's history and has devastated the country's vital tourism sector.
Sabratha, also the site of famed Roman ruins, is known to contain training camps for jihadis.
The March attack on the museum killed 22 people, mostly tourists, and there have been repeated criticisms of the Tunisian government for not doing more to prevent another attack on visiting foreigners.
Earlier, the Tunisian president revealed that the attack took place just days before the country planned to implement heightened security measures for the Muslim fasting month of Ramadan.
However, the plans, which were scheduled to be put into place on July 1, had not anticipated an assault on tourist beaches, Beji Caid Essebsi added.
The attack in Sousse is expected to cripple the vital tourism sector, with the Tunisian tourism minister predicting half billion dollars (£318 million) in losses for 2015.
In an interview with Europe 1 radio, president Essebsi said an investigation into security failures is under way, and there will now be armed tourist police on the beaches. Army reservists have also been called up.
Mr Essebsi said: "It is not a perfect system - it is true we were surprised by this affair.
"They took measures for the month of Ramadan but never did they think the attack would be on the beaches against tourists, and the system of protection was set to start July 1."
Mr Essebsi, 88, is a veteran of Tunisia's pre-revolutionary regime and was elected last autumn on a platform of restoring security and dignity to the state.
The country's vital tourism sector suffered a staggering blow when 24-year-old Rezgui used an assault rifle to kill 38 tourists, mostly Britons. The rampage continued for around half an hour before he was finally shot dead by police. It is Tunisia's worst terror attack.
Since the massacre, authorities have shut 80 unregulated mosques believed to be preaching radical doctrine, and have considered closing organisations and political parties promoting ideas counter to the constitution.
"What is threatened is our way of life and society," Mr Essebsi said, praising Tunisia as one of the only democracies in the region.
"It is not easy being the exception; we should be the model, but it is a model not yet accepted by the others."
He added that Tunisia needs more assistance in securing its frontiers, especially with Libya, home to supporters of the radical Islamic State group which claimed responsibility for the attack.
Tourism minister Samia Elloumi outlined a series of measures to cushion the industry from an expected loss of just over £318 million following the attack.
She said debts and taxes for tourist companies would be rescheduled, new loans guaranteed by the state would be offered and transport taxes for non-resident Tunisians would be cut.
Most important for foreigners, the 15 dollar (£10) departure tax will be scrapped and visa requirements will be been lifted for visitors from China, India, Iran and Jordan.
The current tourism promotion campaign will also be pulled.
Ms Elloumi said: "It would be indecent and not send a positive message to put out such a campaign while the bodies of the victims are still there and people remain in shock."
The poisonous ideology of the so-called Islamic State within the last few days has moved from the seemingly far-off deserts and cities of Iraq and Syria to a popular holiday resort in Tunisia. On the same day at least 27 people were killed by an explosion at a Shia mosque in Kuwait city, and in France a man was beheaded.
In a momentous vote that could change the course of a conflict that had seemed close to a devastating end, the United Nations last night authorised military action against forces loyal to Colonel Muammar Gaddafi and the imposition of a no-fly zone over Libya.