Easily Repeat Root Commands with History
Previously, I mentioned some of the benefits of using the “history” command to display any or all of your previous commands. On dedicated servers, whether Linux or Unix, that use “sudo” rather than “su” to become root, it can be aggravating when you type a long command string only to realize you forgot to type “sudo.”
One easy solution is to press the up arrow, move over to the beginning of the string, and add sudo. But there is an even easier way to replay the command with root permissions.
Right after you have entered the command missing sudo, just type the following:
sudo !!
This will automatically run the last command in the shell history with whatever you place before it (in this case “sudo”). It is quick, easy, and gets the job done. In fact, you can use “!!” any time you want to repeat the previous command. Log in to your server via SSH and give it a try.
Photo Source: Wikimedia Commons
Quick and easy sudo trick for servers
Question: I just typed a really long complicated command line string on my server, but I forgot to type “sudo” at the beginning. Is there a quick way to enter it again?
Answer: The history feature in Linux and Unix-like operating systems is truly a beautiful thing. With it, you can easily re-enter commands. But what do you do if you need to re-enter a command but need to add “sudo” to the beginning? On Ubuntu, Mac OS X, and many other servers, “sudo” is the default method used to gain administrative rights, but it must be entered before each administrative command.
There are two ways to fix this. One is to simply press the up arrow. You will again see your command exactly as you typed it. To add sudo to the beginning, press the “Home” key, which should move the cursor to the front. Then, all you have to do is enter sudo, add a space, and press Enter.
An even quicker method is to use “sudo !!” to automatically reload the command with sudo. This will look to the last command entered in the “history” list and run it again. You can also use “!!” without sudo anytime you want to quickly run a command again.
Image Source: Wikimedia Commons
Tag: command, history, linux, mac os x, server, sudo, unix
Sudo vs. SU

The “su” command is what a Linux user with proper permissions uses to ascend to the root administrator account. A secure SSH server will not allow direct root login, so the user must gain root after initial login. The “sudo” command is an alternative to using a separate root user with its own password. Instead, the user prepends “sudo” before a command that needs root privileges. Certain distributions, such as Ubuntu use “sudo” by default, while others, such as CentOS, prefer “su”.
There are benefits and drawbacks to each. With “su”, root is a true separate user, and some administrators find it more useful to be able to log in as root and run several commands. With “sudo” only commands with those words in front of it will be administrative preventing the user from accidentally running a dangerous command as root.
There are security benefits to each, and the argument over which is better can get heated. Suffice it is to say that it ultimately depends on the preference of the system administrator. Does he/she want to deal with one password or two? Technically, an administrator can decide to use both on the same server and use the one that is most beneficial at the time. It can also be useful if you have multiple users who need various permissions.
Photo Source: Flickr
Tag: .su, administrator, linux, root, secure, server, ssh, sudo
How to restart Apache in Mac OS X Server
Question: How do I restart Apache HTTP Server on a Mac OS X server?
Answer: Linux other Unix-based servers, you can start and stop Apache from the command line. If you are familiar with Redhat-based systems, then you are used to logging in and then using “su” to become Root. Mac OS Xrelies on “sudo” like many Debian-based systems (such as Ubuntu).
The command for Apache in Mac OS X is “apachectl” (Apache control). To restart Apache, type the following:
$ sudo apachectl restart
To stop Apache, enter:
$ sudo apachectl -k stop
And to start Apache again, type:
$ sudo apachectl -k start
After entering a command with “sudo” you will be prompted for your password. Enter it and continue. You will still need to use “sudo” for each command run as Root, but it will not ask you for the password again until you have stopped using it for a period of time in that session.
Source: nixCraft
Tag: apache, linux, mac os x, root, server, sudo, unix, web server