Gandalf walked for many years amongst the Free Folk of Middle-earth but filled utmost of that time amongst the Elves, where he studied much and taught much. He also proceeded to gather such knowledge about Sauron and his ilk as he could, and to that point, he started to suspect that the Necromancer of Dol Guldur remained, in fact, Sauron getting shaped once again. He entered Dol Guldur, but Sauron, still significantly weakened, fled from him and for a short time, the evil of Mirkwood was significantly decreased. However, Sauron immediately returned to Dol Guldur, and by 2850 of the Third Age, had reassembled his powers there. In that year, Gandalf repeatedly entered the fortress and found Thráin II, father of Thorin Oakenshield, who was weakening after having been horribly tortured. Before his death, the old Dwarf king consigned Gandalf with a map to Erebor.
In the year 2941, Gandalf grew troubled by his understanding of Sauron’s resurgent strength. He grasped that Sauron was then plotting war from Dol Guldur, and that as shortly as he felt powerful enough, he would strike Rivendell. Regrettably, the only power left besides Rivendell to counter an effort from Mirkwood to recover the areas of Angmar was the Dwarves of the Iron Hills. Even severely, Gandalf remembered that Smaug to Golden lived still in Erebor and that Sauron could and would use the dragon to destructive effect once he became strong enough to control Smaug. However, Saruman appeared reluctant to engage Sauron’s forces, though none of the White Council realized that he was then a traitor. One night, while considering the problem of the Free People’s vulnerability in the North, Gandalf had a chance meeting in Bree with the Dwarf Thorin II Oakenshield, who was also searching, without way or much hope, for a way to restore the Kingdom Under the Mountain. Gandalf and Thorin then concocted a plan (more Gandalf’s than Thorin’s) to possibly draw the monster out of the mountain and recover the kingdom. To that effect, Gandalf encouraged Bilbo Baggins, a Hobbit of the Shire, to persuade him to go with Thorin and Group to Erebor and support them. At the moment, Bilbo believed of him as little more than a hollow, a fussy old conjurer who delighted children with fireworks when festivals and performances, but soon realized his true nature. It is on this journey that Gandalf obtains his sword, Glamdring, and that Bilbo gains his sword, Sting, and the One Ring (though at the time it is mistaken for a lesser ring).
Gandalf himself joined the quest both to see what he could do to destroy the dragon, and to try to convince the White Council (particularly Saruman) to discharge Sauron from Dol Guldur. Through the Dwarves’ quest, Gandalf disappeared twice — once to explore their path, the second time to “attend to other urging business”, the reality of which he declined to address. He was really participating in a gathering of the White Council. When Bilbo obtained the One Ring, Gandalf was instantly suspect of the Hobbit’s story of how he acquired it. He confidentially confronted Bilbo and urged the truth out of him, and is genuinely troubled by his account of the Ring’s abilities as they seem eerily familiar. Perhaps even more troubling to him is that Bilbo, a decent, honorable Hobbit, would uncharacteristically mislead facts about his story.