Skip to content

How to Install tmate slave on CentOS 7

dingzhengkai edited this page Nov 6, 2017 · 1 revision

How to Install tmate slave on CentOS 7

1- Install required packages for tmate and for compiling

安装 development tools: autoconf automake binutils bison flex gcc gcc-c++ gettext libtool make patch pkgconfig redhat-rpm-config rpm-build rpm-sign

yum groupinstall -y 'Development Tools'

安装编译 tmate-slave 所需依赖

yum install -y git kernel-devel zlib-devel openssl-devel ncurses-devel cmake ruby libssh-devel wget msgpack-devel libevent-devel

检查 libevent 是否安装以及版本是否匹配. 需要安装 libevent2.

2- Installing tmate-slave

编译安装(configure过程中如果提示缺少依赖, 按照提示安装)

git clone https://github.com/nviennot/tmate-slave.git
cd tmate-slave/
./autogen.sh && \
./configure && \
make && \
make install

设置权限

setcap CAP_SETUID,CAP_SYS_ADMIN,CAP_SYS_CHROOT,CAP_SETGID=+ep /usr/local/bin/tmate-slave

3- Configure tmate-slave

setup tmate-slave user

useradd tmate-slave

setup keys

./create_keys.sh
install -d -m 0700 -o tmate-slave -g root /etc/tmate-slave/keys keys/
mv keys/* /etc/tmate-slave/keys
sudo chown -R tmate-slave /etc/tmate-slave

Setup log files

touch /var/log/tmate-slave.{log,err}
chown tmate-slave /var/log/tmate-slave.{log,err}

Setup daemon init script

cat << 'EOF' > /etc/init.d/tmate-slave
#!/bin/sh
### BEGIN INIT INFO
# Provides:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

APP_NAME=tmate-slave
APP_CONF_DIR="/"
APP_USER="$APP_NAME"
APP_CMD="/usr/local/bin/$APP_NAME"
APP_ARG_PORT="22"
APP_ARG_HOST=""
APP_ARG_KEYSDIR="/etc/tmate-slave/keys"


if [ -r /etc/default/$APP_NAME ]; then
  . /etc/default/$APP_NAME
fi

APP_ARGS="-k $APP_ARG_KEYSDIR -p $APP_ARG_PORT "

[ -z $APP_ARG_HOST ] || APP_ARGS="$APP_ARGS -h $APP_ARG_HOST"


name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd "$APP_CONF_DIR"
        #su - "$APP_USER" -c "$APP_CMD $APP_ARGS" >> "$stdout_log" 2>> "$stderr_log" &
        $APP_CMD $APP_ARGS >> $stdout_log 2>> $stderr_log &
        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
    fi
    ;;
    stop)
    if is_running; then
        echo -n "Stopping $name.."
        kill `get_pid`
        for i in {1..10}
        do
            if ! is_running; then
                break
            fi

            echo -n "."
            sleep 1
        done
        echo

        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have failed"
            exit 1
        else
            echo "Stopped"
            if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
    fi
    ;;
    restart)
    $0 stop
    if is_running; then
        echo "Unable to stop, will not attempt to start"
        exit 1
    fi
    $0 start
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0
EOF

chmod +x /etc/init.d/tmate-slave

Setup daemon config

cat << 'EOF' > /etc/default/tmate-slave
export APP_ARG_PORT=""
export APP_ARG_HOST=""
EOF

如果系统的ssh端口是22, 或者有其他程序已经占用了22端口, 需修改上面的配置文件

4- Starting tmate-slave

If SSH is running, select another port and start tmate

sudo service tmate-slave start

5- Configuring the client to use your server

Create $HOME/.tmate.conf with the following

set -g tmate-server-host "[your server FQDN]"
set -g tmate-server-port [server port]
set -g tmate-server-dsa-fingerprint   "dsa fingerprint"
set -g tmate-server-rsa-fingerprint   "rsa fingerprint"
set -g tmate-server-ecdsa-fingerprint "ecdsa fingerprint"
#set -g tmate-identity ""              # Can be specified to use a different SSH key

Problems

1 tmate客户端配置文件$HOME/.tmate.conf中配置的dsa/rsa-fingerprint 不能连接, 只有ecdsa可以, 不明白原因

2 默认启动脚本启动, 连接服务失败, 服务日志fatal: Need root priviledges

修改脚本,使用root用户启动服务--不安全, 暂时没找到其他方法

注释 su - "$APP_USER" -c "$APP_CMD $APP_ARGS" >> "$stdout_log" 2>> "$stderr_log" &

改为 $APP_CMD $APP_ARGS >> $stdout_log 2>> $stderr_log &