Linux - Changing permissions on files and folders
man chmod
will bring up the manual page for the chmod command.
We can search for the text in it.
e.g. To search for recursive
, type /recursive
Reading material
- https://ss64.com/bash/chmod.html
- https://superuser.com/questions/295591/what-is-the-meaning-of-chmod-666
How to make a file executable?
chmod +x somefile.sh
or
chmod u+x somefile.sh
./somefile.sh
To change permissions on all the files in a directory
chmod +x *.sh
How to know if a file is executable?
When you do ls, r
stands for read-only. xr
stands for executable and readable.
[explorer436@explorer436-p50-20eqs27p03 bin]$ ls
total 40
-rwxr-xr-x 1 explorer436 explorer436 5883 Jun 23 08:59 mvn
-rwxr-xr-x 1 explorer436 explorer436 1684 Jun 23 08:59 mvnDebug
-rwxr-xr-x 1 explorer436 explorer436 1611 Jun 23 08:59 mvnyjp
drwxr-xr-x 2 explorer436 explorer436 4096 Jul 21 23:56 .
drwxr-xr-x 6 explorer436 explorer436 4096 Jul 21 23:56 ..
-rw-r--r-- 1 explorer436 explorer436 6324 Jun 23 08:59 mvn.cmd
-rw-r--r-- 1 explorer436 explorer436 2169 Jun 23 08:59 mvnDebug.cmd
-rw-r--r-- 1 explorer436 explorer436 327 Jun 23 08:59 m2.conf
How to change permissions on a directory?
sudo chmod a+x /usr/bin/java
sudo chmod a+x Downloads
How to execute .sh programs?
Pass it to the shell interpreter like this: sh somefile.sh
How to change permissions for directory to allow read/write from anywhere?
Chmod to allow read and write permissions for directory
chmod -R 0777 mydirectory
For all users to have read and write access, that would be 0777 which is a bit dangerous. This will allow all users read and write access to all files and folders within that directory.
Depending on our purpose, we may want to read about sticky bits, which allow all users to create new files, but not to delete or edit other files in a directory:
chmod +t mydirectory
UNPROTECTED key file errors
$ ssh -i '/home/explorer436/Downloads/a-private-key.key' ubuntu@<an-ip-address>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/explorer436/Downloads/ssh-key-2024-07-12.key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/explorer436/Downloads/ssh-key-2024-07-12.key": bad permissions
ubuntu@152.70.202.171: Permission denied (publickey).
[explorer436@explorer436-p50-20eqs27p03 programming-notes]$
The fix to this error is: the keys should be only readable by you (this also blocks your write access): chmod 400 ~/.ssh/id_rsa
Make a file only readable by you
If a file needs to be only readable by you: chmod 400 ~/.ssh/id_rsa
If a file needs to be read-writable by you: chmod 600 ~/.ssh/id_rsa