SOUSSE, Tunisia — At least one gunman disguised as a vacationer attacked this placid Mediterranean resort on Friday, killing at least 38 people at a beachfront hotel — most of them British tourists — before he was shot to death by the security forces.

The slaughter at the Imperial Marhaba Hotel in Sousse, a palm-flecked town on the northeast Tunisian coast, was among the worst terrorist attacks in the country’s history and the second in a little more than three months aimed at the tourist industry, which depends on European vacationers.

It sent shock waves through the country, which was still recovering from the massacre of mostly foreign tourists at a museum in Tunis, the capital, in March.

Roaming from the beach to the pool to the reception area of the hotel, the assailant appeared to have methodically targeted guests with a Kalashnikov rifle he had hidden in a beach umbrella, according to accounts by witnesses, including terrified hotel workers. He was shot and killed by police officers who witnesses said arrived about 30 minutes after the attack began.

There were conflicting accounts on whether the gunman acted alone. The Interior Ministry identified him as a 23-year-old Tunisian student with no police record. The Islamic State claimed responsibility for the attack, according to statements released Friday night on Twitter.