If you are in the US, it ain't gonna happen, Sorry kid, those days are over.
I grew up riding the rails. Mostly the Eastern US. There were some dangers involved in those days but some simple precautions would keep you out of trouble most of the time.
However, today, they monitor the cars with cameras and crack heads are everywhere trying to kill you for the 10 cents in your pocket.
Like everything else in the world (and, certainly this country) the simplicity is gone. What you want to do is a distant fantasy in the 21st century.
I hitched from New York to Cali when I was 17 and that was a much better way to go if you have any social skills at all. Instead of fending for yourself on the rails, you stayed with friendly giving people much of the time.
I doubt hitching is a reality any longer either. Some perv will pick you up and twiddle your diddle.
It's a fucked up world and you were born too late. LOL
Today, it is simply called "going off grid". You can try that but.... damn, good luck. LOL