'Just don't be in the toilet when we dive deep," executive officer David "Bing" Crosby advises me. "The bulkheads bend and you can't open the doors." Forget all the technical stuff about monitoring "the Bubble" when you dive – I never quite understand the role of that mysterious piece of equipment – this is the kind of practical guidance I need. I vow to stick close to Bing, second-in-command on the nuclear submarine HMS Triumph, and a funny, down-to-earth bloke. "It's like having children," he says as the younger officers grapple for space at the first morning briefing I attend. "I've got children, and sometimes I think they're worse." This seems to be meant affectionately.

I have been on the boat – submarines are always called boats, never ships – for less than 24 hours and am writing this log at a depth of 60m. Sorry, I spoke too soon. We are just rising to periscope depth – 18m below the surface – and the tall desk on which I am typing has started to list. At least I'm not in the toilet. I joined HMS Triumph in Crete for the final week of its 10-month deployment. I had never been on a submarine before and don't especially like confined spaces. I plan to get off at Gibraltar six days from now, though the captain warns me this may not be possible if there is fog as the launch can't come alongside.

HMS Triumph distinguished itself last year in Libya, firing missiles at Gaddafi's key installations, including the one which hit the colonel's compound. The submarine's captain, Robert Dunn, feels they have been largely written out of the Libyan campaign and is keen his crew get their due. "If it be a sin to covet honour, I am the most offending soul alive," he tells me over breakfast, quoting Henry V. "Faint hearts never fucked a pig" is another of his maxims, which may be a translation from Clausewitz's On War. The book sits on a narrow shelf in his small cabin beside the control room.

This is Capt Dunn's final command; in fact his very last week at sea. At 48, and after three years in charge of HMS Triumph, he will be getting a desk job. It is also Chief Petty Officer Simon Johnson's final week at sea. There is a nice moment during my week aboard when the submarine (never sub, an egregious Americanism) has to surface to pass through the Straits of Messina. We go through at sunrise and Chief Johnson, who is up on the bridge, tells me it's the first time he has sailed past Stromboli since he was on the frigate HMS Leander at the start of his naval career 30 years ago. The volcano has bookended his life at sea.

I ask Chief Johnson why he switched from surface ships to submarines. "I got drafted in 1983 and didn't have any choice," he says. "I tried to get out of it. I didn't want to be a submariner. If you ask a general service chap what they think about submarines, they'll say: 'Horrible, dirty, noisy, you can't have a shower, you're always stinking.' Well, in the old days that might have been the case – water was very restricted – but you can see yourself; conditions are not that bad. On my first boat, Spartan, at the back end of 1983, it all clicked – it was a completely different way of life from general service."

He goes on to cite what most submariners say is what they like about life beneath the waves: the relative informality. There are, of course, distinctions between officers and ranks, but in so confined a space nothing like the rigidities of surface ships; the sense of being an elite, what one able seaman calls a "brotherhood"; the camaraderie that comes from knowing they rely entirely on each other. When a man, whether officer or rating, becomes a submariner, he is awarded a badge formed from two dolphins and a crown. The badge admits you to an exclusive club – there are around 3,500 operational submariners in the UK. It means that, in the event of an emergency, you will be a help, rather than a hindrance. Until then, in the uncompromising language of submariners, you are an "oxygen thief".

"Everyone who wears the dolphins badge has a common ethos, common training and a common focus," says Lieutenant Stuart Keillor, an impressive 31-year-old Scot who will one day command a submarine. "No one does this job unless they want to. There's a lot of willpower in putting yourself into this environment for a long period. There's a lot of loyalty; and for that reason I'm very comfortable with my shipmates. There's a high expectation of the trainees that come on here. They've got a lot to prove, but once they're in the club, you can be confident they've reached a certain standard."

I know I will never be in the club. Climbing the ladders between decks exhausts me; I am forever hitting my head on protruding bits of metal; and once in the control room, while leaning against the periscope, I stumble backwards and accidentally press a button. Luckily, it is the button that says "search" and not the one next to it that says "attack". I don't want to be responsible for a missile assault on Algeria, to which we happen to be close at the time.

The submarine is two separate worlds: the front half, where the men (and at the moment it is all men, though there are likely to be female submariners from next year) sleep and eat and make war; and the back half, where the nuclear reactor sits beneath the tunnel that separates for'ard from back'aft and where the engines are. I don't spent much time back'aft, but I do visit for a back'afties' treat – potatoes baked on the engine throttle. At a crude level, a submarine crew is divided into those who make war and those who make the engines work. "We push, they fight," as one back'aftie explains succinctly.

The engineers call the nuclear reactor "the big kettle". It is what enables one of these fast-attack submarines to go on long operations, with no need to come into port for refuelling. Having it on board is a huge responsibility, as HMS Triumph's marine engineering officer, Lieutenant Commander Andy Sharp, makes clear. "It's never going to explode," he says when I ask him what's the worst thing that could happen, "but it could melt. If a nuclear submarine had an accident that caused it to have a slump and melt and drop out the bottom of the boat, I don't think we'd have a nuclear fleet any more. That would be the end of nuclear submarines. It's that level of responsibility." That pressure is enormous, but it works: to its credit, the navy has managed, over half a century, to run a fleet of submarines without a major incident.

HMS Triumph's cramped conditions add to the claustrophia. Photograph: Gary Calton for the Guardian

A key distinction to grasp is between fast-attack submarines such as HMS Triumph, which are armed with conventional cruise missiles (the UK has seven such boats in varying degrees of readiness), and the four nuclear-armed submarines, one of which is always on patrol, ready to unleash its Trident nuclear missiles if apocalypse beckons. The latter – called ballistic submarines or bombers – are, at 180m long, almost twice the size of Triumph, have bigger crews and are, according to the men who have served on them, deadly boring. "Their job is to stay silent," says one petty officer. "You have no contact with the outside world." The bombers stay at a constant depth, move very slowly and do everything to avoid detection. It is three months of suspended animation; 180 men aspiring to the life of a flat fish, though a highly educated one – many are doing Open University degrees to pass the time and improve themselves.

Another man tells me the crew on board one bomber used to amuse themselves by pretending to be motorbikes, and this rings true. "Spinning a dit" is a phrase you hear a lot on board HMS Triumph. It means telling a tale – a tale that grows with the telling. Even on a fast-attack sub, if there is nothing to attack and you grow tired of listening to passing whales and pretending to target nearby destroyers, life can get repetitive, so the men slip into other worlds. One of the crew has a large tattoo on his back: "Never stop me dreaming," which might stand as a motto for all of them.

One evening, I wander into the control room at about midnight. The watch officer and sonar operators are discussing an important philosophical question: would it be more painful to be struck by a whole tuna or a tin of tuna? This is never resolved. These epistemological issues can be sustained over weeks.

On my fourth day aboard, I make my greatest discovery: that a badger, washed into the bilge tank in Bahrain, is being kept back'aft. There is a roster to feed it, and somehow it is being kept alive. I insist on seeing it – what a wonderful story! Of course, say the back'afties, come by this evening. After a couple of hours, even in my dim-witted, mind-clouded, headachy state, I realise I have been conned. Do they even have badgers in Bahrain?

But the fantasy has become important to some of the crew. "It helps pass the time," the head badger-keeper tells me. "It takes the edge off the situation," says one of the senior ratings. "If you get into a situation that's a bit tense, mentioning the badger brings everything back down to earth. If you tried to remain alert 100% of the time it would fatigue you. The ability to relax allows you, when required, to be on the ball." It's the ability to switch instantly from badger mode to potential nuclear meltdown mode that defines a good crew.

This is a highly segregated society, yet also a very organic one. There are three separate messes, for officers, senior ratings and junior ratings, each situated on the short corridor that serves as the men's living space. The separate messes with their different atmospheres – the Xbox is never off in the junior rates mess – suggest division, yet everything else implies unity. The pay structure is relatively flat: Capt Dunn earns around £85,000; the most junior rating gets £30,000. What other organisation has that sort of ratio between top and bottom? And every crew member, officer or rating, has to know everything about the boat – the function of every one of the thousands of valves. There are half a dozen trainees on the boat studying for their dolphin badges, and they are forgoing all sleep to memorise the handbook they have been given in time for a test that could be sprung on them at any time.

Chief Petty Officer Paul "Jakie" Foran, the likable but occasionally terrifying Scot who oversees these tests, expects dedication, and woe betide any trainee (AKA oxygen thief) who is discovered having a cup of tea in the junior rates' mess when he could be unearthing the secret of the magazine spray drench system. "To me you're useless until you're qualified," says Chief Foran. "I'm a bastard, but in a nice way."

You learn early whether you will survive in this world. One young officer who wants to transfer from surface ships is aboard studying for his dolphins, and is reckoned to have too many airs and graces. The crew are merciless in mocking this affront to their democratic values. He expects to be shown where every valve is and what it's for; don't be absurd (or words to that effect), says Chief Foran, you must find out for yourself. Forget sleep.

Forgetting sleep is easy. The crew work 12 hours a day, split into six-hour watches, with changeovers at 1 and 7. Back'afties, because of the heat in which they're working, have shorter but more frequent shifts. When they're not working, most men will be in their "rack", but sleeping on a submarine is no fun. The captain, alone on the boat, gets his own cabin; the senior officers share; and everyone else is in hot, cramped, fetid dormitories. Bed space is so limited that some of the most junior ratings have to "hot bunk", sleeping in the bed vacated by a man who has just gone on watch.

You can hardly move in the bunk – sitting up is impossible – and if you turn over you are likely to tip out and end up on the floor. You have to share your rack with a gas mask and various other bits of safety equipment, plus a lot of your own gear. There are small lockers, but I am never offered one, so sleep with bag, clothes and shoes in the bed. Each bunk has an air vent, which does offer some respite from the heat but also blows a blast of cold air into your right ear. "If the air stops blowing, it means something bad has happened," one of the men tells me reassuringly. One morning I am woken by a sudden thud and fear the worst. Later, I discover it was just air being released – a routine operation.

TCPO ‘Tug’ Wilson in the weapons room (AKA ‘bomb shop’). Crew members often hug the cruise missiles to stay cool while they sleep. Photograph: Gary Calton for the Guardian

Several men mention "coffin dreams" – nightmares in which the sleeper shouts out that the control room is flooding or he is being pursued by a torpedo. I sympathise: though I have no nightmares – I don't sleep deeply enough for that – the racks do feel like coffins. A better option is to sleep in the "bomb shop", where the missiles and torpedoes are kept. It is the quietest, most spacious room on the boat and hugging an 18ft cruise missile keeps you cool.

A few men go "wibble" after years under water; they just can't stand it any more – the lack of proper sleep, the absence of privacy, the endlessly repeated conversations, the cycle of meals (it's Wednesday so it must be curry), the unspoken dangers. How do you know when someone has gone wibble? "The noisy ones go quiet, and the quiet ones suddenly become noisy," one man tells me. Chief Johnson recalls one experienced submariner who went wibble and started keeping a book of shipmates he thought had wronged him. "You're on my list as well," he told Johnson before being taken off the boat. He only agreed to leave as long as he could be designated captain of the rescue vessel.

The men who go doolally are, however, the exceptions. Most get into a routine – working, sleeping (as best they can), reading (Kindles are a godsend in this confined world), watching films, spinning dits and dripping. Dripping is another key part of the submariner's lexicon: it means moaning, usually in a lighthearted way and often about the food. "Ah, you found the one mushroom in the mushroom soup," says Lieutenant Gareth Batsford, who as well as being the resident film buff in the officers' mess is also one of the boat's aphorists. "You wouldn't believe the inability of well-educated, well-trained Royal Navy officers to change bloody toilet rolls," he drips at one point.

I penetrate many aspects of submarine life in my six days aboard, but sex remains elusive. "There's a saying – what happens at sea stays at sea," says medical assistant Richard Bastianpulle teasingly. "I've not really thought about it, except when I'm in bed … by myself. People let off steam when they come alongside. I'll let you read between the lines on that." The boat will usually come into port every two months or so to pick up provisions, and the men – the single men at least – can get quite frisky.

There is one openly gay man on board. "They don't treat me any different to anyone else," he says. "I didn't tell anyone at first. I let them ask me rather than me tell them. But it's better to be open and honest than to try and deny it. People will catch you out and they'll start spreading malicious rumours." He gets ribbed about his sexuality, but says everyone gets ribbed about something. "Ribbing doesn't bother me. I let it go over my head. Some days it'll get to you, but you have to brush it off and forget about it."

The key thing is that bantering must never turn into bullying. The navy, which has become alert to equality and diversity in the past decade, is now good at stamping out bullying, which would destroy the esprit de corps in a community this small and close-knit. Christopher Herbert, a 34-year-old from St Vincent, who I discover up in the dry provisions store mixing his own rap song (The Vagina Rap) on a computer, says he has never suffered racial abuse on the boat. "It's zero tolerance," he says.

On my last night in the officers' mess we watch Operation Petticoat, an old submarine film with Cary Grant and Tony Curtis. They must have seen it a dozen times, but love it. The officers – especially Lieutenant Commander Crosby, who misses his young children hugely – cry easily during movies. Andy Sharp gives his recipe for the sort of movie that plays well on board, especially with the back'afties: "Some token violence, some top bollocks on view from time to time, definitely some love interest, a car chase, and it must have a happy ending." Most of these teak-like submariners, even Chief Foran, are softies at heart.

I do manage to get off at Gibraltar, two miles off the coast in a heavyish swell. A dinghy comes alongside, bashing against the side of the submarine, and one of the crew pushes me into it. The timing of the leap is crucial. Get it wrong and you will be dripping in a literal sense. I end up upside down in the dinghy and wrench my shoulder. I'm just not built to be a submariner, and seem to lack Cary Grant's swagger. I am loaded on to a small patrol vessel, which pipes its salutation to the departing submarine. I am relieved to be breathing fresh air, yet sad not to be heading home with HMS Triumph. As a parting gift, the captain has given me an honorary dolphins badge, and I will treasure it always. Despite all my shortcomings, I feel I was starting to become part of the life of the boat. Me and the badger.