SSH的安装配置和使用
在Solaris 8以及之前的版本中系统里由于没有自带SSH(Secure Shell),需要手动安装才能使用,Solaris 9之后,系统都自带SSH,可直接使用。在Solaris 8中安装SSH,需要的软件包括OpenSSH、OpenSSL、Gcc,这些都可从http://www.sunfreeware.com上下载到最新的版本。安装过程不再累述。安装后需要进行一些配置,如下:1、设置PATH
安装之后可执行文件位于目录/usr/local/bin下,为方便使用,最好在$PATH中加入该目录。需要注意的是,SSH的守护进程sshd位于/usr/local/sbin目录下,因为启动守护进程时需要以绝对路径执行,所以不用设置该PATH。
2、如有需要,对sshd_config进行配置
配置文件sshd_config位于/usr/local/etc/目录下。
默认使用Protocol 1和2,如果只使用Protocol2,可将#Protocol 2,1这一行的注释符#删除,将该行修改为Protocol 2,并将HostKey /usr/local/etc/ssh_host_key这一行注释掉。
如果想不允许root使用ssh登录,可将PermitRootLogin yes这一行的yes改成no。
3、生成dsa、rsa等key文件
执行如下命令:
# ssh-keygen -t dsa -f /usr/local/etc/ssh_host_dsa_key -N ''
# ssh-keygen -t rsa -f /usr/local/etc/ssh_host_rsa_key -N ''
如果第2步中使用的是默认的Protocol 2,1,还需要生成ssh_host_key文件,命令如下:
# ssh-keygen -t rsa1 -f /usr/local/etc/ssh_host_key -N ''
4、创建sshd用户
如果没有sshd用户,sshd的守护进程会无法运行,sshd用户不需要shell,home目录为/var/empty,按如下步骤创建:
# mkdir /var/empty
# chown root:sys /var/empty
# chmod 755 /var/empty
# groupadd sshd
# useradd -g sshd -c 'sshd privsep' -d /var/empty -s /bin/false sshd
5、启动sshd守护进程
启动sshd守护进程时需要以绝对路径来执行:
# /usr/local/sbin/sshd
6、设置开机启动时自动启动sshd守护进程
1)在/etc/init.d目录下创建sshd启动脚本,脚本可参照下面这个:
#!/sbin/sh
KEYDIR=/usr/local/etc/
KEYGEN="/usr/local/bin/ssh-keygen -q"
PIDFILE=/var/run/sshd.pid
case $1 in
'start')
if [ -x /usr/local/bin/ssh-keygen ]; then
if [ ! -f "$KEYDIR/ssh_host_rsa_key" ]; then
echo "Creating new RSA public/private host key pair"
$KEYGEN -f $KEYDIR/ssh_host_rsa_key -t rsa -N ''
fi
if [ ! -f "$KEYDIR/ssh_host_dsa_key" ]; then
echo "Creating new DSA public/private host key pair"
$KEYGEN -f $KEYDIR/ssh_host_dsa_key -t dsa -N ''
fi
fi
[ -x /usr/local/sbin/sshd ] && /usr/local/sbin/sshd &
;;
'stop')
if [ -z "$_INIT_RUN_LEVEL" ]; then
set -- `/usr/bin/who -r`
_INIT_RUN_LEVEL=""
_INIT_PREV_LEVEL=""
fi
if [ $_INIT_RUN_LEVEL -lt $_INIT_PREV_LEVEL ]; then
/usr/bin/pkill -u 0 -x sshd
fi
if [ -f "$PIDFILE" ]; then
/usr/bin/kill -TERM `/usr/bin/cat $PIDFILE`
fi
;;
'restart')
if [ -f "$PIDFILE" ]; then
/usr/bin/kill -HUP `/usr/bin/cat $PIDFILE`
fi
;;
*)
echo "Usage: $0 { start | stop }"
exit 1
;;
esac
2)更改脚本文件的属性
# chown root:sys sshd
# chmod 744 sshd
3)在/etc/rc3.d和/etc/rc1.d目录下创建链接
# ln -s /etc/init.d/sshd /etc/rc3.d/S99StartSSHD
# ln -s /etc/init.d/sshd /etc/rc1.d/K99StopSSHD
4)这样,就实现了开机启动sshd守护进程了,也可手动启动、停止、重置sshd进程
# /etc/init.d/sshd start
# /etc/init.d/sshd stop
# /etc/init.d/sshd restart
7、ssh登录
到这里,就可以在各个机器之间ssh登录了,如使用账号user1从hosta登录到hostb(前提是在两台机器上都需存在user1的账号,否则用ssh -l切换到其他用户登陆),在hosta上执行如下操作:
$ /usr/local/bin/ssh hostb
The authenticity of host 'hostb' can't be established.
RSA key fingerprint in md5 is: 43:3a:94:56:73:18:a0:6a:a4:77:d0:ba:71:bc:14:cc
Are you sure you want to continue connecting(yes/no)?yes
Warning: Permanently added 'hostb,192.168.0.112' (RSA) to the list of known hosts.
user1@hostb's password:(输入user1账号在hostb上的密码)
Last login: Wed Apr 22 11:41:12 2009 from hosta
Sun Microsystems Inc. SunOS 5.8 Generic Patch February 2004
$
8、如果想要实现不输入密码直接从hosta登陆到hostb,按如下步骤操作:
1)在hosta上以user1创建rsa(也可以使用dsa)的公钥/私钥对,直接回车取默认值:
$ /usr/local/bin/ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/export/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /export/home/user1/.ssh/id_rsa.
Your public key has been saved in /export/home/user1/.ssh/id_rsa.pub.
The key fingerprint is:
8c:b9:fd:27:4f:52:a7:20:96:db:64:20:f9:aa:01:fe user1@hosta
2)拷贝公钥至hostb:
$ /usr/local/bin/scp ~/.ssh/id_rsa.pub user1@hostb:~/.ssh/authorized_keys
user1@hostb's password:
id_rsa.pub 100% |*********************************| 218 00:00
如果hostb上user1的home目录下没有.ssh目录,scp会失败,需要手动创建一个,记得把权限改为700。
3)现在就可以不输入密码直接用ssh从hosta登录到hostb了。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/80598/showart_1907150.html
回复 #1 geriwolf 的帖子
请问在redhat下也使用相同的过程??
页:
[1]