SSH vs WordPress

Checking what permissions you are actually setting is always a good idea before just doing so.

644 = Owner Read/Write, Group Read, Others Read
664 = Owner Read/Write, Group Read/Write, Others Read

4 – Read, 2 – Write, 1 – Execute

You can see from sequence above 644 the first number 6 would be 4+2, Read+Write. It’s always OWNER/GROUP/OTHERS, so first number owner, second group, etc.

Set files to 664, and directories to 775. I actually wrote a script a few years ago to do this automatically (you may need to change 644 to 664 and 755 to 775):
https://github.com/tripflex/scripts/blob/master/chmodwp

You would need to set write permissions for the group www-data which is why use 664 and 775.

Do you only get this issue when you do edit files via SSH? There’s a couple things you could do if the issue was the user/group, including using runuser

http://www.cyberciti.biz/open-source/command-line-hacks/linux-run-command-as-different-user/

Could just setup an alias for when you edit files, if that’s what you’re doing.

The problem you’re having is because WordPress can’t write to the files on your server. Make sure your parent directory www or public_html is set to the right user:group and has the right permissions.