Linux客户端使用SSH的命令管理linux服务器

<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } TD P { margin-bottom: 0cm } A:link { so-language: zxx } -->

登陆:ssh [hostname] 

输入密码:*****

登 陆以后就可以像控制自己的机器一样控制它了,不过没有可视化的界面。不过现在 我所使用过的两个版本Linux(SUSE FC5) 中有可以使用类似FTP 界面的工具。使用工具连接时,选择SSH ,端口是填服务器的SSH 端口,默认是22 ,但是还是要手工填写。连接上以后FTP 界面和shell 同时使用可以 提高工作效率。

 

# visudo

%wheel ALL=(ALL) ALL

# gpasswd -a user1 wheel

4) 限制 ssh 使用者名单

# vi /etc/pam.d/sshd

auth required pam_listfile.so item=user sense=allow file=/etc/ssh_users ōnerr=fail

# echo user1 >> /etc/ssh_users

5) 封锁 ssh 联机并改用 web 控管清单

# iptables -I INPUT -p tcp --dport 22 -j DROP

# mkdir /var/www/html/ssh_open

# cat > /var/www/html/ssh_open/.htaccess < AuthName "ssh_open"

AuthUserFile /var/www/html/ssh_open/.htpasswd

AuthType basic

require valid-user

END

# htpasswd -c /var/www/html/ssh_open/.htpasswd user1

( 最好还将 SSL 设起来, 或只限 https 联机更佳, 我这里略过 SSL 设定, 请读者自补.)

( 如需控制联机来源, 那请再补 Allow/Deny 项目, 也请读者自补.)

# cat > /var/www/html/ssh_open/ssh_open.php < //Set dir path for ip list

$dir_path=".";

//Set filename for ip list

$ip_list="ssh_open.txt";

//Get client ip

$user_ip=$_SERVER['REMOTE_ADDR'];

//allow specifying ip if needed

if (@$_GET['myip']) {

$user_ip=$_GET['myip'];

}

//checking IP format

if ($user_ip==long2ip(ip2long($user_ip))) {

//Put client ip to a file

if(@!($file = fopen("$dir_path/$ip_list","w+")))

{

echo "Permission denied!!

";

echo "Pls Check your rights to dir $dir_path or file $ip_list";

}

else

{

fputs($file,"$user_ip");

fclose($file);

echo "client ip($user_ip) has put into $dir_path/$ip_list";

}

} else {

echo "Invalid IP format!!

ssh_open.txt was not changed.";

}

?>

END

# touch /var/www/html/ssh_open/ssh_open.txt

# chmod 640 /var/www/html/ssh_open/*

# chgrp apache /var/www/html/ssh_open/*

# chmod g+w /var/www/html/ssh_open/ssh_open.txt

# chmod o+t /var/www/html/ssh_open

# service httpd restart

# mkdir /etc/iptables

# cat > /etc/iptables/sshopen.sh < #!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

list_dir=/var/www/html/ssh_open

list_file=$list_dir/ssh_open.txt

chain_name=ssh_rules

mail_to=root

# clear chain if exits, or create chain.

iptables -L -n | /bin/grep -q "^Chain $chain_name" && {

iptables -F $chain_name

true

} || {

iptables -N $chain_name

iptables -I INPUT -p tcp --dport 22 -j $chain_name

}

# clear chain when needed

[ "$1" = clear ] && {

iptables -F $chain_name

exit 0

}

# do nothing while list is empty

[ -s $list_file ] || exit 1

# add rule

iptables -A $chain_name -p tcp --dport 22 -s $(< $list_file) -j ACCEPT && /

echo "ssh opened to $(< $list_file) on $(date)" | mail -s "sshopen" $mail_to

END

# chmod +x /etc/iptables/sshopen.sh

# echo -e 'sshopen/t/t1234/tcp' >> /etc/services

# cat > /etc/xinetd.d/sshopen < service sshopen

{

disable = no

socket_type = stream

protocol = tcp

wait = no

user = root

server = /etc/iptables/sshopen.sh

}

# iptables -I INPUT -p tcp --dport 1234 -j ACCEPT

# cat > /etc/cron.d/sshopen < */5 * * * * root /etc/iptables/sshopen.sh clear

END

---------------------------

转往 client

browser URL 输入:

http://server.machine/ssh_open/ssh_open.php?myip=1.2.3.4

( 若不指定 ?myip=1.2.3.4 则以 client 当时 IP 为准, 若没经 proxy 的话.)

如此, server 端的 ssh_open.txt 文件只有单一记录, 每次盖写.

接着:

$ telnet server.machine 1234

然后你有最多 5 分钟时间用 ssh 联机 server !

---------------------------

此步骤的基本构思如下:

5.1) sshd firewall 联机全部 block .

5.2) 然后在 httpd 那设一个 directory, 可设 ssl+htpasswd+allow/deny control,

然后在目录内写一个 php browser ip 记录于一份 .txt 文字文件里.

视你的转写能力, 你可自动抓取 browser 端的 IP, 也可让 browser 端传入参数来指定.

文字文件只有单一记录, 每次盖写.

5.3) 修改 /etc/services , 增加一个新项目(xxx), 并指定一个新 port(1234)

5.4) 再用 xinetd 监听该 port , 并启动令一只 scrīpt, 设定 iptables , step2 的清单里取得 IP, 为之打开 ssh 联机.

5.5) crontab 每数分中清理 iptables 关于 ssh 联机的规则. 这并不影响既有联机, 若逾时再连, 则重复上述.

6) 要是上一步骤没设定, 你或许会担心过多的人来 try 你的 ssh 服务的话:

# cat > /etc/iptables/sshblock.sh < #!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

LOG_FILE=/var/log/secure

KEY_WORD="Illegal user"

KEY_WORD1="Failed password for root"

PERM_LIST=/etc/firewall/bad.list.perm

LIMIT=5

MAIL_TO=root

IPT_SAV="$(iptables-save)"

bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs)

bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs)

bad_list="$bad_list $bad_list1"

for i in $(echo -e "${bad_list// //n}" | sort -u)

do

hit=$(echo $bad_list | egrep -o "$i" | wc -l)

[ "$hit" -ge "$LIMIT" ] && {

echo "$IPT_SAV" | grep -q "$i .*-j DROP" || {

echo -e "/n$i was dropped on $(date)/n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO

iptables -I INPUT -s $i -j DROP

}

egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST

}

done

END

# chmod +x /etc/firewall/sshblock.sh

# cat >> /etc/hosts.allow < sshd: ALL: spawn ( /etc/firewall/sshblock.sh )& : ALLOW

END

这样, 那些乱 try SSH 的家伙, 顶多能试 5 (LIMIT 可调整), 然后就给 BLOCK 掉了.

此外, PERM_LIST ip, 也可提供给 iptables 的初始 scrīpt , 来个永久性封闭:

for i in $(< $PERM_LIST)

do

/sbin/iptables -I INPUT -s $i -j DROP

done

7) 还有, 你想知道有哪些人对你做 full range port scan 的话:

# iptables -I INPUT -p tcp --dport 79 -j ACCEPT

cat > /etc/xinetd.d/finger < service finger

{

socket_type = stream

wait = no

user = nobody

server = /usr/sbin/in.fingerd

disable = no

}

END

# cat >> /etc/hosts.allow < in.fingerd: ALL : spawn ( echo -e "/nWARNING %a was trying finger./n$(date)" | mail -s "finger from %a" root ) & : DENY

END

这里, 我只是设为发信给 root.

事实上, 你可修改为起动 firewall %a 这个传回值给 ban 掉也行.

不过, 对方要是有选择性的做 port scan , 没扫到 finger 的话, 那当然就没用了...

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值