SSH安装
SSH分客户端openssh-client和openssh-server
如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-get install openssh-client)
如果要使本机开放SSH服务就需要安装openssh-server
$ sudo apt-get install openssh-server
然后确认sshserver是否启动了:
$ ps -e |grep ssh
如果看到sshd那说明ssh-server已经启动了。
如果没有则可以这样启动:
sudo /etc/init.d/ssh start
如果提示用 service ssh start,则用
sudo service ssh start
SSH使用
1 启动
$ service ssh start
2 停止
$ service ssh stop
3 重启ssh
$ service ssh restart
输出参考:
ubuntu@ubuntu12:~/workspace$ sudo /etc/init.d/ssh restart
[sudo] password for ubuntu:
Rather than invoking init scripts through /etc/init.d, use the service(8) utility, e.g. service ssh restart Since the script you are attempting to invoke has been converted to an Upstart job, you may also use the stop(8) and then start(8) utilities, e.g. stop ssh ; start ssh. The restart(8) utility is also available. ssh stop/waiting ssh start/running, process 11688提示说应该用 service ssh restart 来重启
ubuntu@ubuntu12:~/workspace$ sudo service ssh restart
ubuntu@ubuntu12:~/workspace$ sudo service ssh restart ssh stop/waiting ssh start/running, process 13348
4 登陆和退出ssh(以登陆本机的ssh为例)
$ ssh localhost
ubuntu@ubuntu12:/home/svn$ ssh localhost The authenticity of host 'localhost (127.0.0.1)' can't be established. ECDSA key fingerprint is e1:4c:b9:16:ec:c3:3b:3b:e9:91:e3:e2:20:41:33:5e. Are you sure you want to continue connecting (yes/no)? 【输入yes】 Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts. ubuntu@localhost's password: 【输入密码】 Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686) * Documentation: https://help.ubuntu.com/ 296 packages can be updated. 131 updates are security updates. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. ubuntu@ubuntu12:~$ exit 登出 Connection to localhost closed.
SSH服务端配置
ssh-server配置文件位于/ etc/ssh/sshd_config,
在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。然后重启SSH服务:
##开头的是我加的备注,#是原文的注释,没有#号的是配置项
主要是参考了http://blog.licess.org/sshd_config/
# Package generated configuration file # See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for ## 端口 Port 22 # Use these options to restrict which interfaces/protocols sshd will bind to ## 如果你的服务器有多个IP地址,可以通过下面的配置制定IP和用什么协议,一般来说都是用ssh2 #ListenAddress :: #ListenAddress 0.0.0.0 ## 举个例子来说,如果您有两个 IP, ## 分别是 192.168.0.100 及 192.168.2.20 ,那么只想要 ## 开放 192.168.0.100 时,就可以写如同下面的样式: ## ListenAddress 192.168.0.100 ## 只监听来自 192.168.0.100 这个 IP 的SSH联机。 ## 如果不使用设定的话,则预设所有接口均接受 SSH Protocol 2 ## 选择的 SSH 协议版本,可以是 1 也可以是 2 , ## 如果要同时支持两者,就必须要使用 2,1 这个分隔了! ## Protocol 2,1 # HostKeys for protocol version 2 ## 指定ssh2使用的私钥的存放位置, HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key #Privilege Separation is turned on for security ## 使用者的权限设定项目!就设定为 yes 吧! UsePrivilegeSeparation yes # Lifetime and size of ephemeral version 1 server key ## 使用协议1时,会使用到server的公钥,如果这个公钥被偷的话,岂不完蛋?所以需要每隔一段时间重新生成一个,默认为3600秒 KeyRegenerationInterval 3600 ## 密钥的长度 ServerKeyBits 768 # Logging ## 当有人使用 SSH 登入系统的时候,SSH会记录下来,这个信息要记录在什么 daemon name 底下? ## 预设是以 AUTH 来设定的,即是 /var/log/secure里面!什么?忘记了!回到Linux 基础去翻一下 ## 其它可用的 daemon name 为:DAEMON,USER,AUTH,LOCAL0,LOCAL1,LOCAL2,LOCAL3,LOCAL4,LOCAL5, SyslogFacility AUTH ## 记录的日志等级LogLevel INFO# Authentication: ## 安全相关的设置,很重要。。。 ## 登陆设置相关 ## LoginGraceTime 120 ## 是否允许 root 登入!预设是允许的,但是建议设定成 no! PermitRootLogin yes ## 当使用者的 host key 改变之后,Server 就不接受联机,可以抵挡部分的木马程序! StrictModes yes ## 这个是协议版本1用的,使用纯的RSA认证 RSAAuthentication yes ## 这个是协议版本2用的,是否允许公钥, PubkeyAuthentication yes ## 若要使用不需要密码登入的账号时,那么这些帐号信息存放在哪个文件 #AuthorizedKeysFile %h/.ssh/authorized_keys ## 认证设置相关 # Don't read the user's ~/.rhosts and ~/.shosts files ## 是否取消使用 ~/.ssh/.rhosts 来做为认证!当然是! IgnoreRhosts yes # For this to work you will also need host keys in /etc/ssh_known_hosts ##这个选项是专门给 version 1 用的,使用 rhosts 档案在/etc/hosts.equiv配合 RSA 演算方式来进行认证!不要使用 RhostsRSAAuthentication no # similar for protocol version 2 ## 这个项目与上面的项目类似,不过是给 version 2 使用的! HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication ##是否忽略home目录内的 ~/.ssh/known_hosts 这个档案所记录的主机内容?当然不要忽略,所以这里就是 no 啦! #IgnoreUserKnownHosts yes # To enable empty passwords, change to yes (NOT RECOMMENDED) ##若PasswordAuthentication=yes 的话,这一项就最好设定为 no ,这个项目在是否允许以空的密码登入!当然不许! PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with# some PAM modules and threads) ## 挑战任何的密码认证!所以,任何 login.conf 规定的认证方式,均可适用! ChallengeResponseAuthentication no # Change to no to disable tunnelled clear text passwords ##密码验证当然是需要的!所以这里写 yes 啰!或者注释掉也可以 #PasswordAuthentication yes # Kerberos options ## Kerberos 这个没用过,是一种主机 #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes ## 跟X11相关的设置 X11Forwarding yes X11DisplayOffset 10 ## 登入后是否显示出一些信息呢?例如上次登入的时间、地点等等,预设是 yes ,但是,如果为了安全,可以考虑改为 no ! PrintMotd no ## 显示上次登入的信息!可以啊!预设也是 yes ! PrintLastLog yes ## 一般而言,如果设定这项目的话,那么 SSH Server 会传送KeepAlive 的讯息给 Client 端,以确保两者的联机正常! ## 在这个情况下,任何一端死掉后, SSH 可以立刻知道!而不会有僵尸程序的发生! TCPKeepAlive yes ## SSH不支持使用Login的登录 #UseLogin no ## 同时允许几个尚未登入的联机画面 #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variablesAcceptEnv LANG LC_* ## 关于SFTP的设置,打开了这一项目才可以用ssh传输文件 Subsystem sftp /usr/lib/openssh/sftp-server # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. UsePAM yes