Archive for the 'linux' Category

Persistent SSH sessions

The screen program, among other useful things, lets you keep an SSH session running even after you disconnect from SSH. Here’s how to use it.

SSH in.

ssh user@hostname.com

Once on the remote machine, set up a named screen:

screen -S myscreen

In another terminal, open another SSH connnection and start another screen:

screen -S mysecondscreen

You can see they are there by using, in either of the terminals,

screen -ls

This does NOT start screen, just lists the different screens.

You can now disconnect the SSH connections. When you reconnect, you can use

screen -r myscreen

or

screen -r mysecondscreen

to reconnect to the one you want.

Setting up and using an SVN repository

Install subversion (Ubuntu)

sudo apt-get install subversion

Make a directory to store the repositories

mkdir /path/to/repository

Create the repository

svnadmin create /path/to/repository

Import existing files into repository

svn import /path/name/to/foo file:///path/to/repository

when you checkout this repository, it will create the directory foo. So to get the svn repository in my ~/work directory as ~/work/foo I would go to ~/work, then

svn co file:///path/to/repository

that is, don’t make a new dir dir called foo and import into there . . . it will make its own dir.

Check out locally

svn checkout file:///path/to/repository /local/workdir

Check out remotely through an ssh connection

svn checkout svn+ssh://user@hostname/path/to/repository/on/remotehost /local/workdir

Update local copy from SVN

svn update

Check what’s been changed

svn status

Resolve a conflict

svn resolved filename

Send these changes to SVN (editor will prompt for revision notes, must be non-empty)

svn commit

Send these changes to SVN, and specify logfile to send as comments

svn commit -F logfile

A more secure SSH configuration

Some easy ways to configure SSH to be a little more secure:

Edit /etc/ssh/sshd_config as root.

  • Change the port (default is 22)
  • Change “PermitRootLogin yes” to “PermitRootLogin no”
  • AddUser username
  • save and quit
  • restart the ssh server: sudo /etc/init.d/ssh restart

More info here:
http://ubuntu-tutorials.com/2007/02/14/what-you-ought-to-know-about-securing-ssh/

SSH: Add public key to remote server

Easy way to add public key of this machine to a remote machine:

ssh-copy-id -i ~/.ssh/id_dsa.pub root@fileserver01

More good info here:
http://ubuntu-tutorials.com/2007/02/05/unattended-ssh-login-public-key-ssh-authorization-ssh-automatic-login/

Remote MySQL using SSH

Step 1: SSH forwarding

First, forward the local port 3307 to 3306. That is, when you access the local port 3307, it will redirect it to port 3306 on the remote host.

ssh -fNg4 -L 3307:127.0.0.1:3306 user@hostname

-f sends SSH to the background
-g allows remote hosts to connect to local forwarded ports
-N don’t execute a remote command
-4 this was key! Forces IPv4. Kept getting “bind: Address already in use” errors because I didn’t have this.
-L the forwarding magic happens here . . . syntax is localport:localhost:remoteport

Step 2: Connect to mysql on port 3307

. . . which will redirect to port 3306 on remote host.

mysql -u root -h 127.0.0.1 -P 3307 -p

and you’re in!

Check open ports

Are there any open ports that shouldn’t be open? Check with:

sudo netstat -tupl

Results in something like:


Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address  State    PID/Program name
tcp        0      0 localhost:mysql  *:*              LISTEN  5077/mysqld
tcp        0      0 localhost:ipp    *:*              LISTEN  5001/cupsd
udp        0      0 *:32768          *:*                      5324/avahi-daemon:
udp        0      0 *:bootpc         *:*                      5875/dhclient
udp        0      0 *:mdns           *:*                      5324/avahi-daemon: 

Kill the process that is using the port

kill (PID here)

List open files:

lsof -i

where the -i makes it list internet files.

bash script: convert images

Convert all eps files into jpeg copies in current directory and lower.

#!/usr/bin/bash
for f in $(find -type f -iname '*.eps')
do
     dest=`echo ${f%.*}`
     echo "${f} to ${dest}.jpg"
     convert "${f}" "${dest}.jpg"
done

bash script: find and remove files

This bash script will find all the files matching the pattern *foo.txt.

#!/usr/bin/bash
for f in $(find -type f -name '*foo.txt')
do
    echo "removing ${f}."
    rm "${f}"
done

Use -iname for case-insensitive, which will even match hidden files (files with a leading dot).
The -type f means only find regular files.

Configure SSH

sudo vim /etc/ssh/sshd_config 

Restart SSH

sudo /etc/init.d/ssh restart