Getting 'permission denied error' with Wget

I installed the LAMP stack on my Ubuntu 11.04 (Natty Narwhal) with

sudo apt-get install lamp-server^

Now I navigate to /var/www and use

wget http://ftp.drupal.org/files/projects/drupal-7.x-dev.tar.gz

I get the following error.

 --2011-08-03 13:59:00-- 
 http://ftp.drupal.org/files/projects/drupal-7.x-dev.tar.gz Resolving
 ftp.drupal.org... 64.50.233.100, 64.50.236.52 Connecting to
 ftp.drupal.org|64.50.233.100|:80... connected. HTTP request sent,
 awaiting response... 200 OK Length: 2757101 (2.6M)
 [application/x-gzip] drupal-7.x-dev.tar.gz: Permission denied

 Cannot write to `drupal-7.x-dev.tar.gz' (Permission denied).

How do I resolve this error?

I tried to download the same file in the downloads folder, and it went on without an issue. So it seems to be some permission error, but I'm not sure about that or how to solve it.


Solution 1:

I believe you need root permissions to write to /var/www. So you should use sudo in front of your command i.e

sudo wget http://ftp.drupal.org/files/projects/drupal-7.x-dev.tar.gz

Solution 2:

You don't need to sudo every time you get some error; this does not fix the problem. The problem is, as the error says that you don't have "permission" to "write" to that directory.

Of course, root has the right to do anything, which the primary goal of many viruses happen to be. I have chmod 755 & chmod 766 to get the same error with wget, cp & mv.

What worked was to own the directory being worked in so don't have to sudo often:

sudo chown <username> <directory>

After this, ordinary commands work normally.

Tested on Ubuntu 16.04.7 LTS