A shell script went wild due to some bug, and the script overwrote a binary file /bin/ping. Here is how tor restores it.
/bin/ping erased (Credit: https://dribbble.com/TonyBabel)
There are two ways to solve this problem.
Easy way: copy it from another server
Just scp file from another box running the same version of your Linux distribution:
$ sudo scp vivek@server1.cyberciti.biz:/bin/ping /bin/ping
Proper way: Search and reinstall package
First, query the package which provides FILE /bin/ping as per your Linux distro:
Debian/Ubuntu Linux user type
$ dpkg -S /bin/ping
iputils-ping: /bin/ping
Now just reinstall the iputils-ping package using apt-get command or apt command:
$ sudo apt-get --reinstall install iputils-ping
RHEL/SL/Scientific/Oracle Linux user type
$ yum provides /bin/ping
iputils-20071127-24.el6.x86_64 : Network monitoring tools including ping
Now just reinstall the iputils package using yum command:
$ sudo yum reinstall iputils
Fedora Linux user type
$ dnf provides /bin/ping
iputils-20161105-1.fc25.x86_64 : Network monitoring tools including ping
Now just reinstall the iputils package using dnf command:
$ sudo dnf reinstall iputils
Arch Linux user type
$ pacman -Qo /bin/ping
/usr/bin/ping is owned by iputils 20161105.1f2bb12-2
Now just reinstall the iputils package using pacman command:
$ sudo pacman -S iputils
Suse/OpenSUSE Linux user type
$ zypper search -f /bin/ping
Sample outputs:
Loading repository data...
Reading installed packages...
S | Name | Summary | Type
--+---------+------------------------------------+--------
| ctdb | Clustered TDB | package
i | iputils | IPv4 and IPv6 Networking Utilities | package
| pingus | Free Lemmings-like puzzle game | package
Now just reinstall the iputils package using zypper command:
$ sudo zypper -S iputils
If everything else failed, try restoring the file from backup
The reason why you keep backups is here. It is time to restore that file from NAS server or tape device:
scp vivek@backup1.cyberciti.biz:/backups/server1.cyberciti.biz/snap/lastest/bin/ping /bin/ping
What can be done to avoid such problem in future?
Testing in a sandbox is an excellent way to prevent such problem. Care must be taken to make sure that variable has value. The following is dangerous:
echo "foo" > $file
Maybe something like as follows would help (see “If Variable Is Not Defined, Set Default Variable“)
file="${1:-/tmp/file.txt}"
echo "foo" > $file
Another option is to stop if variable is not defined:
${Variable?Error \$Variable is not defined}
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 3 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
The easiest way: if you place a “set -u” in your script then bash shows an error when it tries to use an undefined variable.
It would be nice to also include that if a binary gets deleted, but is still running, one can (in most cases) recover it from /proc/$pid/exe . Although it may seem to be a dangling link, the read/copy operations still work.
Another way is to use Python instead of shell scripting, where Python will detect major issues like undefined variables.