He entered a naval academy in 1832 and received a warrant in the United States Navy as a midshipman in 1835, was promoted to passed midshipman in 1841, the first of 22 passed by the board of examination. By 1843, he was acting master of the USS Vincennes. He was promoted to lieutenant in 1847. His postings included the USS Potomac, USS Columbia, USS Vincennes, USS Cyane, the Norfolk Naval Shipyard, and USS Susquehanna. In 1861, at the beginning of the AmericanCivil War, he held the position of first lieutenant on the USS Susquehanna in the Mediterranean Sea. Upon returning to the United States on June 6, he resigned his commission the same day.
Barney was appointed a lieutenant in the Confederate States Navy on July 2, 1861. He commanded the CSS Jamestown during the Battle of Hampton Roads, the famous battle with the, during which he captured two brigs and an Accomac schooner off Newport News Point while the CSS Virginia held the Union Navy's attention. After the Confederate evacuation of Norfolk, the Jamestown was scuttled to block the James River at Drewry's Bluff. In the subsequent Battle of Drewry's Bluff, in which Confederate shore batteries drove off a Union Navy force heading towards Richmond, the Jamestown's guns were considered the main factor in repulsing the enemy ships. Following the battle, he received praise from the Confederate Congress and was promoted to commander. In early 1863, Barney was briefly assigned command of naval operations in Galveston, including the revenue cutter Harriet Lane, captured in the Battle of Galveston. However the ship was already under command of Leon Smith, an army volunteer and steamboat captain, who had played a role in capturing the ship, having been placed in command of the ship by Major General John B. Magruder and in control of additional ships improvised as a "cottonclad fleet". The ship was also considered by the navy to be too slow and inefficient to become a blockade runner, though it was later used in this capacity. Following discussions with Magruder, who was not willing to relinquish controls of the cottonclads, Barney conceded the appointment, and in a letter to Confederate Naval Secretary Stephen Mallory, recommended that the navy relinquish control. Barney later explained that he made his recommendation since he considered that the presence of two separate marine forces with independent commanders would lead to discord and confusion. Barney was sent to Europe in spring 1863. He assumed command of the Confederate raider CSS Floridain September 1863, relieving the ailing John Newland Maffitt, and oversaw her refit at Brest, France, but had to be detached due to ill health before the Florida put to sea. From 1864 to 1865, he was a Confederate naval agent in Europe. He returned to the United States following the war, and took the Oath of Allegiance to the U.S. Constitution in September 1865.