Tip: uid 501 does not exist in the passwd file!

I had some trouble doing sudo on a remote (OS X 10.5) machine, logged in via ssh. It turned out that if I do
ssh username@machinename.local
then I cannot do sudo on that machine (getting the error "uid 501 does not exist in the passwd file!"). Also I can no do su (getting the error "su: who are you?"). However, if I log in using
ssh -l username machinename.local
everything works fine. Just in case you stumble across this problem...