Numbers 20:1-13
The Following Rock
1 Then came the children of Israel, even the whole congregation, into the desert of Zin in the first month: and the people abode in Kadesh; and Miriam died there, and was buried there. 2 And there was no water for the congregation: and they gathered themselves together against Moses and against Aaron. 3 And the people chode with Moses, and spake, saying, Would God that we had died when our brethren died before the LORD! 4 And why have ye brought up the congregation of the LORD into this wilderness, that we and our cattle should die there? 5 And wherefore have ye made us to come up out of Egypt, to bring us in unto this evil place? it is no place of seed, or of figs, or of vines, or of pomegranates; neither is there any water to drink.
6 And Moses and Aaron went from the presence of the assembly unto the door of the tabernacle of the congregation, and they fell upon their faces: and the glory of the LORD appeared unto them. 7 And the LORD spake unto Moses, saying, 8 Take the rod, and gather thou the assembly together, thou, and Aaron thy brother, and speak ye unto the rock before their eyes; and it shall give forth his water, and thou shalt bring forth to them water out of the rock: so thou shalt give the congregation and their beasts drink.
9 And Moses took the rod from before the LORD, as he commanded him. 10 And Moses and Aaron gathered the congregation together before the rock, and he said unto them, Hear now, ye rebels; must we fetch you water out of this rock? 11 And Moses lifted up his hand, and with his rod he smote the rock twice: and the water came out abundantly, and the congregation drank, and their beasts also.12 And the LORD spake unto Moses and Aaron, Because ye believed me not, to sanctify me in the eyes of the children of Israel, therefore ye shall not bring this congregation into the land which I have given them. 13 This is the water of Meribah; because the children of Israel strove with the LORD, and he was sanctified in them.
Professor James Kugel explains the growth of the legend of the travelling rock (or "following" rock):
. . . why did Scripture now say that "these are the waters of Meribah"; weren't the waters of Meribah back in Rephidim, many miles to the west? Interpreters noticed another thing as well: from the time when Moses had struck the rock at Rephidim, in the book of Exodus, to this moment at Kadesh, in the book of Numbers, there had not been a word about the Israelites lacking water. At the same time, Scripture did not report on their reaching any new oasis or digging any new wells.
. . . it must have been that they continued to have their needs supplied by that very same rock that Moses had struck. This was their source of drinking water for a full forty years. And if Scripture said that the "waters of Meribah," which had been located at Rephidim, were now found at Kadesh, that must mean that the rock that provided those waters had somehow moved (or been transported) from Rephidim to their present location, so as to be constantly in proximity of the Israelites and their flocks.
Thus was born the tradition of the traveling rock that followed Israel in its desert wanderings for forty years . . ."
James L. Kugel, How to Read the Bible: a Guide to Scripture Then and Now, p.238f
See Exodus 17:6 and Paul's use of the legend of the "following rock" at 1 Corinthians 10:4.