By Joe Hemba

DAMATURU, Nigeria (Reuters) - Suspected Islamist militants stormed a college in northeastern Nigeria and shot dead around 40 male students, some of them while they slept early on Sunday, witnesses said.

The gunmen, thought to be members of rebel sect Boko Haram, attacked one hostel, took some students outside before killing them and shot others trying to flee, people at the scene told Reuters.

"They started gathering students into groups outside, then they opened fire and killed one group and then moved onto the next group and killed them. It was so terrible," said one surviving student Idris, who would only give his first name.

"They came with guns around 1 a.m. (2400 GMT) and went directly to the male hostel and opened fire on them ... The college is in the bush so the other students were running around helplessly as guns went off and some of them were shot down," said Ahmed Gujunba, a taxi driver who lives by the college.

Boko Haram, which wants to establish an Islamic state in northern Nigeria, has intensified attacks on civilians in recent weeks in revenge for a military offensive against its insurgency.

Several schools, seen as the focus of Western-style education and culture, have been targeted.

Boko Haram and spin-off Islamist groups like the al Qaeda-linked Ansaru have become the biggest security threat in Africa's second largest economy and top oil exporter.

Western governments are increasingly worried about the threat posed by Islamist groups across Africa, from Mali and Algeria in the Sahara, to Kenya in the east, where Somalia's al-Shabaab fighters killed at least 67 people in an attack on a Nairobi shopping mall a week ago.

Bodies were recovered from dormitories, classrooms and outside in the undergrowth on Sunday, a member of staff at the college told Reuters, asking not to be named.

A Reuters witness counted 40 bloody corpses piled on the floor at the main hospital in Yobe state capital Damaturu on Sunday, mostly of young men believed to be students.

The bodies were brought from the college, which is in Gujba, a rural area 30 miles (50km) south of Damaturu and around 130 miles from Nigerian borders with Cameroon and Niger.

State police commissioner Sanusi Rufai said he suspected Boko Haram was behind the attack but gave no details.

REVENGE ATTACKS

Thousands have been killed since Boko Haram launched its uprising in 2009, turning itself from a clerical movement opposed to Western culture into an armed militia with growing links to al Qaeda's West African wing.

President Goodluck Jonathan declared a state of emergency in three northeastern state in May, including Yobe, and ordered a military offensive to crush Boko Haram's insurgency.

There was an initial lull in the violence as Islamists fled bases in cities, forests and mountains. Then the militants began revenge attacks on schools, security forces and civilians believed to be helping them.

In July, suspected Boko Haram militants killed 27 students and a teacher at a school in Potiskum, a town about 30 miles from the site of Sunday's attack.

Several hundred people have died in assaults over the past few weeks. Some observers say the army offensive has only succeeded in pushing attacks away from well-guarded large towns and cities into vulnerable rural areas.

Boko Haram's insurgency is also putting pressure on the economy of Africa's most populous nation. Nigeria's security spending has risen to more than 1 trillion naira ($6.26 billion) per year, or around 20 percent of the federal budget. ($1 = 159.8 Nigerian naira)

(Additional reporting by Isaac Abrak in Kaduna; Writing by Joe Brock; Editing by Andrew Heavens)