In the spring of 1941, Al Woods (Andy Griffith) quits an Oklahoma college to join the armed forces after a quarrel with his co-ed sweetheart, Jo Hill (Erin O'Brien). He joins the Coast Guard, partly by chance due to the flip of a coin.
Al makes it through basic training, but a run-in with one of his instructor's lands him on the Coast Guard Cutter Periwinkle, a buoy tender, as a junior cook. The ship is homeported in Boston. He encounters immediate hostility from the chief of the galley, Red Wildoe (Walter Matthau), from new crew mates, cooks' helpers Sidney Gutsell (Joey Bishop) and Poznicki (Claude Akins), and from his arrogant department head, Lieutenant (junior grade) Higgins (Ray Danton).
In a bar, Al picks up Stella Papparonis (Felicia Farr), who appears to do this kind of thing with some regularity. They develop a strong attraction, but she seems to be holding out for something more.
Al befriends Gutsell by fixing him up with a girlfriend of Stella's and learns from Wildoe how to be a cook, but in the process he makes a number of embarrassing mistakes.
A frustrated Al stops seeing Stella, whereupon he and the alcoholic Wildoe get drunk together and bond. Wildoe begins seeing Stella, with Al's blessing.
Pearl Harbor is attacked and war declared. Wildoe abruptly proposes to Stella and they get married. A free-for-all breaks out at their wedding celebration, as a jealous Al instigates a fight with soldiers who are clearly familiar with Stella already.
Wildoe is assigned to another vessel performing convoy duty at sea. While he's gone, Stella begins seeing other men. Al tries to prevent this on Wildoe's behalf, but can't resist Stella himself.
Aboard the Periwinkle, Al becomes the new chief cook. Higgins, promoted to Lt. and assigned as the executive officer, is entering lesser amounts in the ledger than the ship pays for the cost of officers' meals and is pocketing the difference. He purchases substandard food for the crew to keep the mess budget from showing a deficit. Higgins also claims to find Al's hair in his food and orders him to start wearing a hat in the galley.
One of Al's shipmates examines his hair and claims that Al is starting to molt, so he shaves Al's head and starts applying what he claims is a restorative liquid. Al earns the nickname "Onionhead" for the way he looks.
Assuming that all the officers are in on the scam, Al bypasses channels to report his suspicions directly to the District Office.
During leave to attend his father's funeral, Al reconnects with Jo, realizing that she is the one he loves. He asks her to marry him.
In port again, Wildoe asks Al to take Stella home from the bar one night when he is recalled to his ship. Stella tries to seduce Al, who calls her a tramp. She replies: "I can't help what I am."
Responding to a ship in distress, the Periwinkle picks up some men in a life raft before engaging a German submarine in a surface battle. Al mans the 20 mm gun and shoots some men on the deck of the submarine before the CO orders the Periwinkle to ram and sink the submarine.
Al regrets having sent a letter to the District Office when he realizes that only Higgins was responsible for embezzling. A board of investigation is sent to the ship and when they interview Al, he denies being able to prove his allegations, choosing to avoid creating a scandle. Instead, he hands the CO a copy of the ledger proving the embezzlement.
In a meeting with Al and the executive officer, the captain tells Al that his punishment for an unsubstantiated allegation against an officer is loss of his rating and reassignment to the Greenland Pool, but he also informs Higgins that he will have to repay every embezzled dollar before his court-martial.
The captain gently chastises Al for not trusting him and coming to him with the proof earlier, but gives him leave to go find and marry Jo before he ships out for Greenland.