Question

New user can't mkdir ~/go/src in Ubuntu 18.04

Per https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04 I created a new user with sudo privileges who can successully ssh in:

root@dev: adduser tom
root@dev: # Give him sudo privileges
root@dev: usermod -aG sudo tom
root@dev: # Copy keys to this account
root@dev:$ rsync --archive --chown=tom:tom ~/.ssh /home/tom

Then I exit to my local OS, and ssh back in as the new user, tom.

The only other thing I did was install Golang:

tom@dev: cd ~
tom@dev: curl -O https://dl.google.com/go/go1.12.1.linux-amd64.tar.gz
tom@dev: sudo tar -xvf go1.12.1.linux-amd64.tar.gz -C /usr/local
tom@dev: sudo chown -R root:root /usr/local/go
tom@dev: # Must have used sudo on these but not sure???
tom@dev: mkdir -p $HOME/go/{bin,src}
tom@dev: nano ~/.profile
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
tom@dev: # Reload profile
tom@dev: . ~/.profile

When I go to create a new directory for a project in Go I get Permission denied:

tom@dev: mkdir ~/go/src/foo
mkdir: cannot create directory ‘foo’: Permission denied

I can do it with sudo but that’s tiring and doesn’t work right anyhow–if I try to nano main.go in the new directory I can’t save because I don’t have privileges.

What did I do wrong that my new user has sudo privileges but can’t even create file or directory without it?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@tomcampbell

I created a new droplet and replayed all of your commands without any errors to make sure you can create directories as normal user do this command

sudo chown -R tom:tom $HOME

To make sure tom is the owner of your home directory and all of its sub directories

Hope this helps

THAT DID IT!!! Thank you, @Mohsen47! Obviously the problem line was where I gave root, which I didn’t really understand was a user and not a privilege level, permissions–not tom.

What a huge relief!

Wait, but now I have another question. What is the difference between these two commands? Is that giving the user tom sudo privileges has NOTHING to do with the $HOME directory? I would assume that sudo privileges would just naturally be granted to that directory, but I’m beginning to suspect that’s nothing close to the truth.

# Give tom sudo privileges...
$ usermod -aG sudo tom

# ...but tom still can't even write to his
# own directory without this. WTF?
$ sudo chown -R tom:tom $HOME

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel