With rsync
rsync
is available by default on Ubuntu ... In --archive, -a
mode, it should only set the permissions (since the file will never change) ... i.e. when used in one shot like so:
$ sudo rsync -a --chmod=755 /bin/chmod /bin/chmod
With the install
command
There is another utility called install
from GNU core utilities as well which should be available by default on your system … It’s mainly used for copying just compiled files and make them executable i.e. install them and hence the name … It should give the copied file the permissions of rwxr-xr-x
by default ... It also has the option -m
to alter permissions of that file selectively on-the-fly (while you might not need that option in this case as the defaults should suffice), but you can always do:
$ sudo install -m +x /bin/chmod mychmod
Then, you can simply do:
$ sudo ./mychmod 755 /bin/chmod
With the dynamic linker/loader
Another simple solution as well is to use the dynamic linker/loader itself ... This is a special way of running binaries as it doesn’t require permissions to do so (It is somewhat similar to running a non-executable shell script by passing it as an argument to /bin/sh
for example) … Depending on the architecture your binary was compiled for, you most likely want to use the amd64
variant ... Find its name and path on your system with for example:
$ dpkg -S 'ld-linux*'
manpages: /usr/share/man/man8/ld-linux.8.gz
manpages: /usr/share/man/man8/ld-linux.so.8.gz
libc6:amd64: /lib64/ld-linux-x86-64.so.2
libc6:i386: /lib/i386-linux-gnu/ld-linux.so.2
libc6-i386: /lib32/ld-linux.so.2
libc6:i386: /lib/ld-linux.so.2
libc6:amd64: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
Then, simply use it in one shot like so:
$ sudo /lib64/ld-linux-x86-64.so.2 /bin/chmod 755 /bin/chmod
Else ...
Search your system for other utilities that can change/set file permissions ... One way is to search the manuals with e.g. man -K "chmod"
... Here are some of what I found on my system:
chacl
(change the access control list of a file or directory):
$ sudo chacl u::rwx,g::r-x,o::r-x /bin/chmod
bwrap
(container setup utility):
$ sudo bwrap --die-with-parent --bind / / --chmod 755 /bin/chmod -- /bin/true