In order to conveniently run commands as root, without needing to login separately as root, or use "su" and enter a password for every command, you should set up sudo on your system.
Setting up sudo
1. Obtain a root shell by some other means, such as logging in as root or using "su".
2. Install the package "sudo" with your package manager of choice.
3. Add a line for yourself to /etc/sudoers:
echo 'username ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
With sudo set up as shown above, you can run any command as root by simply prefixing it with "sudo ". Try the following to test that sudo is working correctly:
If sudo is set up correctly, you should see the id of root rather than yourself.
There is one exception, however. If you are using redirection to read or write a file for which you need root permissions, just prefixing with "sudo " won't help, since redirections are performed by your current shell. You will need to explicitly run a shell and provide a quoted command. For example:
sudo bash -c 'echo hello > /root/filename'
Alternatively, you can use "tee" and a pipe to write output to a file:
echo hello | sudo tee /root/filename
| sudo tee in place of
| sudo tee -a in place of
When you run a command with sudo, $HOME remains the same, allowing you to use your normal settings for programs. If you want to run a command and use root's $HOME, use "sudo -H command".
You can also run a command as any other user, by using "sudo -u otheruser command".