Hartmann was born on 11 December 1902 in Silstedt near Wernigerode in the Province of Saxony, a province of the Kingdom of Prussia in the German Empire. He was the third child of Albert Hartmann, an evangelicpastor in Wernigerode, and Helene Hartmann, née Wernicke. Hartmann had one older brother, Rudolf, and an older sister, Edith. From 1909 to 1914, he attended the Volksschule, a primary school, in Silstedt and later the Gymnasium, a secondary school, in the district of Magdeburg. On 1 April 1914, Hartmann joined the Königlich Preußischen Kadettenkorps in Oranienstein near Diez. He initially served in the Vorkorps before he transferred to the Hauptkadettenanstalt in Berlin-Lichterfelde on 1 April 1917. There he graduated with his Abitur in 1921. Following his graduation he began his naval career with the Reichsmarine of the Weimar Republic on 1 April 1921 as a member of "Crew 21". Until 30 September 1925, he underwent a number of military and naval training courses. It began with basic military training in Stralsund at the Baltic Sea and weapons courses at the Naval Academy at Mürwik. Hartmann was then transferred to the for onboard training and then to the training shipNiobe. During this timeframe, he advanced in rank to Fähnrich zur See on 1 April 1923 and to Oberfähnrich zur See on 4 April 1925. He went on to serve as commander of the torpedo boats and, before transferring to the U-boat arm in 1935. During the Spanish civil war, he commanded that patrolled Spanish waters during the Civil War in 1937–38 with Günther Prien as his first watch officer.
World War II
From January–May 1940 Hartmann was commander of both and 2nd U-boat Flotilla, but directing U-boats while at sea proved inefficient, and the Befehlshaber der U-Boote decided henceforth to direct the U-boats from land. After three patrols, and sinking 19 ships totalling, Hartmann received the Knight's Cross of the Iron Cross. His senior officers on his four patrols on U-37 were future Knight's Cross winners, first watch officer Oberleutnant zur SeeErnst Bauer and later Oberleutnant zur SeeNicolai Clausen, second watch officer Leutnant zur SeeGustav Poel and chief engineer Oberleutnant Gerd Suhren.
First patrol
Hartmann's first patrol on U-37 left Wilhelmshaven almost two weeks before the outbreak of World War II on Friday 1 September 1939 when German forces invaded Poland. Commander of U-37 on this patrol was Kapitänleutnant Heinrich Schuch.
Ashore
Hartmann then moved to the BdU as a staff officer, and in November 1940 became commander of the 2nd ULD. A year later he took command of the 27th U-boat Flotilla in Gotenhafen. In November 1942 he took command of the large Type IXD for a patrol to the Indian Ocean lasting 200 days, the third longest patrol ever undertaken, and sank 7 ships totalling. Chief engineer was Johann-Friedrich Wessels who received the Knight's Cross for his services on this patrol. In 1944 Hartmann became Führer der Unterseeboote Mittelmeer and in this post received the Knight's Cross with Oak Leaves.