Category Archives: Shell Tips

Tips on getting more out of your shell session.

Clearing Linux’s Memory Cache

I saw the following command today posted on a Linux related website:

sudo sh -c “sync; echo 3 > /proc/sys/vm/drop_caches”

While yes, it will clear the memory cache, I question the wisdom of doing this. Linux has really good memory and cache management already, swapping pages in and out as needed. If your system is running low on physical memory it will appropriate memory back from the cache as needed. In 14 years of using Linux I’ve never felt the need to clear the cache.

Working with Screen

Source: http://www.ghacks.net/2010/05/14/some-cool-linux-tipstricks/
Collaborate with the screen command

Let’s say you and your co-worker are both working on debugging an application and your partner has run into a snag. Instead of having to march back and forth between offices, or leaving the comfort of your own chair (you lazy thing), you can log onto your co-workers machine and collaborate! Here’s how it works. Both machines will be Linux machines and both will have ssh accessibility. If you need to help your co-worker out follow these steps:

1. Secure shell into your friends machine…the only catch is, you have to log on with his username/password.
2. Issue the command screen -s NAME Where NAME is any name you want.

Now your co-worker has to open up a terminal window and issue the command screen -x NAME Where NAME is the same name you used in your command. Now whenever either of you type in your screen both of you will see what is going on at the same time.

To detach yourself from this session you have to hit a and then d. That means hold down , hit a, and then hit d. You will be released from the session.

Using PuTTY Without Password Prompting

PuTTY is a great SSH terminal progam for Windows. Using this tutorial you’ll be able to auto login to a remote server without being prompted for a username or password. Do NOT do this in a public machine.

1. Download PuTTY from http://the.earth.li/~sgtatham/putty/latest/x86/putty-0.60-installer.exe

2. Install and then run PuTTYgen from the programs menu.

3. Select SSH2-DSA from the bottom and hit the Generate button. Move the mouse a bit and hit ctrl-c to copy the key.

4. Click on Save Private Key. Click Yes to save it without a passphrase. I would put it in your “My Documents” directory, but you can place it wherever. Make sure you save it with a .ppk extension.

5. Open the Pageant  program for the PuTTY menu. It will put in icon in the system tray. Right click on it and click on Add Keys. Now navigate to where you saved the key file, select it, and hit Open.

6. Lauch PuTTY and type in the hostname to connect to. Type the name again in the Saved Sessions. Then click on the Data option under Connections and put your username in the Auto-login field. Click on Session at the top then click Save to save the session.

7. Click Open and PuTTY will connect to the server and display “Using Username “<your user>”. Put in your password.

8. If you’ve never used SSH from your target server, you will now need to “ssh localhost” so your .ssh directory will be created and with the right permissions. If you had to ssh localhost, now type exit to get back to your original shell and cd into the .ssh dir.

9. Use your favorite text editor to create “authorized_keys2” and paste your key into it (right or middle mouse button).

10. Permissions on authorized_keys2 will probably be -rw-rw-r– so you will need to type “chmod g-w authorized_keys2” to remove group write access, otherwise authorized_keys2 will not be used by ssh.

11. Close your shell session and open PuTTY again. If you did this right then you will now be able to double click on your host and connect without being prompted for a username or password. Another option to open the host is to right click on the Pageant tray icon and click on Saved Sessions to open up a comonly access host.

12. You will now need to get the Pageant program launched upon Windows Startup so you don’t have to open it every time. This can be done by (at least in Vista) by going to the Start Orb, All Progams, PuTTY, right click on Pageant and select copy. Now scroll down and right click on Startup and click Open. Now right click in the empty white space and select Paste Shortcut.

13. You will probably want to start Pageant at windows startup and/or start pageant with your saved key loaded. To do so, right click on the Pageant shortcut, click on properties, then change the target to something like this – “C:\Program Files\PuTTY\pageant.exe” “C:\Documents and Settings\ptrost\My Documents\key.ppk”

14. The quickest way to retrieve the key if you want to add it to another server and it’s not in your copy/paste buffer) is to open up PuTTYgen again and click Load, then select your previously saved key file. Then you can right click in the public key box to select all then again to copy.

Using Bash through the WWW

This tip came from a friend of mine:

I thought you might find something I did useful.  It grants root shell access via a browser using xinetd.  Just DO NOT put it on a public box without securing the www.bash script first (firewall port 50000 through iptables, or acls in xinetd). It sets up an xinetd service on port 50000 and anything after the trailing slash is treated as a shell command.  So http://<SERVER_IP>:50000/ls generates a directory listing of the current directory.

The port can be changed to whatever port > 1024 you want to use.

1.) Add following line to the end of /etc/services
bashweb      50000/tcp                       # bashweb

2.) nano -w /etc/xinetd.d/bashweb
service bashweb
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/www.bash
log_on_failure += USERID
disable = no
port = 50000
}

3.) nano -w /usr/sbin/www.bash

#!/bin/bash

read request

while /bin/true;
do
read header
[ "$header" == $'\r' ] && break;
done


url="${request#GET }"
url="${url% HTTP/*}"
url=`echo $url | sed 's/\///'`
#url=`echo $url | sed 's/%20/ /g'`
#echo $url;


x=`echo $url | sed "s/\/$//" | awk -F "/" '{print $1}'`
#### Change $y below to your desired password.
#### The URL then looks like this: http://<IP>:50000/PASSWORD/<COMMAND>
y='PASSWORD'

if [ $x = $y ]; then
url=`echo $url | sed "s/\/$//" | awk -F "/" '{print $2}'`
echo -e "HTTP/1.1 200 OK\r"
echo -e "Content-Type: text/plain; charset=us-ascii\r"
echo -e "\r"
$url
echo -e "\r"
else
echo "nope"
fi

4. Set executable permisson

chmod 700 /usr/sbin/www.bash

5.) Restart xinetd

service xinetd restart

Browse to http://<SERVER_IP>:50000/PASSWORD/ls -la
to test
Enjoy!

Kyle