阅读 154

SSH 命令介绍(ssh命令详解)

ssh命令多用于登录和文件传输,安全可靠,本文记录ssh命令用法。

概述

SSH(远程连接工具)连接原理:ssh服务是一个守护进程(demon),系统后台监听客户端的连接,ssh服务端的进程名为sshd,负责实时监听客户端的请求(IP 22端口),包括公共秘钥等交换等信息。

  • SSH服务端由2部分组成:

    • openssh(提供ssh服务)

    • openssl(提供加密的程序)

  • SSH是安全的加密协议,用于远程连接Linux服务器

  • SSH的默认端口是22,安全协议版本是SSH2

  • SSH服务器端主要包含2个服务功能SSH连接和SFTP服务器

  • SSH客户端包含ssh连接命令和远程拷贝scp命令等

工作机制

  • 服务器启动的时候自己产生一个密钥(768bit公钥)

  • 本地的ssh客户端发送连接请求到ssh服务器

  • 服务器检查连接点客户端发送的数据和IP地址,确认合法后发送密钥(768bits)给客户端

  • 此时客户端将本地私钥(256bit)和服务器的公钥(768bit)结合成密钥对key(1024bit),发回给服务器端

  • 建立连接通过key-pair数据传输。

加密技术

  • 加密技术:传输过程,数据加密。

    • SSH1没有对客户端的秘钥进行校验,很容易被植入恶意代码

    • SSH2增加了一个确认联机正确性的Diffe_Hellman机制,每次数据的传输,Server都会检查数据来源的正确性,避免黑客入侵。

  • SSH2支持RSA和DSA密钥

  • DSA(digital signature Algorithm):  数字签名

  • RSA: 既可以数字签名又可以加密

命令参数

ssh        [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]            [-D [bind_address:]port] [-e escape_char] [-F configfile]            [-i identity_file] [-L [bind_address:]port:host:hostport]            [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]            [-R [bind_address:]port:host:hostport] [-S ctl_path]            [-W host:port] [-w local_tun[:remote_tun]]            [user@]hostname [command] 复制代码

功能用法

安装 ssh 服务端

sudo apt install openssh-server 复制代码

启动ssh服务

sudo service ssh start 复制代码

检查是否启动成功:

ps -aux | grep ssh 复制代码

如果出现下方标黄部分的sshd 就说明启动成功

安装 ssh 客户端

sudo apt install openssh-client 复制代码

登录

ssh -p 22 username@servername 复制代码

例如:

ssh root@192.168.25.137 ls -ltr /backup/data 复制代码

查看已知主机

 cat /root/.ssh/known_hosts 复制代码

开机自动启动 ssh 服务

  • 通过chkconfig可以将sshd加入到系统服务中。

  • ubuntu 使用 chkconifg 需要额外的安装配置,参考Ubuntu 没有 chkconfig 服务解决办法

sudo chkconfig sshd on 复制代码

  • 查看sshd的运行级别状态:

chkconfig --list sshd 复制代码

$ sudo chkconfig --list sshd sshd         2:on 3:on 4:on 5:on 复制代码

以后重启之后,就可以直接通过ssh远程访问了。

ssh远程执行sudo命令

ssh -t omd@192.168.25.137 sudo rsync hosts /etc/ 复制代码

传输文件

从服务器上下载文件
scp username@servername:/path/filename /var/www/local_dir(本地目录) 复制代码

例如: scp root@192.168.0.101:/var/www/test.txt 把192.168.0.101上的/var/www/test.txt 的文件下载到/var/www/local_dir(本地目录)

上传本地文件到服务器
scp /path/filename username@servername:/path   复制代码

例如: scp /var/www/test.php root@192.168.0.101:/var/www/ 把本机/var/www/目录下的test.php文件上传到192.168.0.101这台服务器上的/var/www/目录中

从服务器下载整个目录
scp -r username@servername:/var/www/remote_dir/(远程目录) /var/www/local_dir(本地目录) 复制代码

例如: scp -r root@192.168.0.101:/var/www/test /var/www/

上传目录到服务器
scp -r local_dir username@servername:remote_dir 复制代码

例如:scp -r test  root@192.168.0.101:/var/www/  把当前目录下的test目录上传到服务器的/var/www/ 目录

ssh自带的sftp功能

Window和Linux的传输工具
wincp  filezip           sftp -->基于 ssh 的安全加密传输 复制代码

sftp客户端连接
sftp -oPort=22 root@192.168.25.137                    put /etc/hosts /tmp                    get /etc/hosts /home/omd  复制代码

sftp小结
  • linux下使用命令: sftp -oPort=22 root@x.x.x.x

  • put加客户端本地路径上传

  • get下载服务器端内容到本地

  • 远程连接默认连接用户的家目录

后台服务

查询openssl软件

rpm -qa openssh openssl 复制代码

查询sshd进程

ps -ef | grep ssh     --> /usr/sbin/sshd 复制代码

查看ssh端口

netstat -lntup | grep ssh   ss | grep ssh                (效果同上,同下,好用) netstat -a | grep ssh(记住这个) netstat -lnt | grep 22    ==>  查看22端口有没有开/ssh服务有没有开启 技巧: netstat -lnt | grep ssh | wc -l -->只要大于2个就是ssh服务就是好的 复制代码

如果没有 netstat 这个命令,需要安装 net-tools

apt-get install net-tools 复制代码

查看ssh的秘钥目录

ll /root/.ssh/known_hosts  # 当前用户家目录的.ssh目录下 复制代码

ssh的配置文件

cat /etc/ssh/sshd_config    复制代码

ssh服务的关闭

service sshd stop 复制代码

ssh服务的开启:

service sshd start 复制代码

ssh服务的重启

service sshd reload    [停止进程后重启] ==> 推荐 service sshd restart   [干掉进程后重启] ==> 不推荐 复制代码

ssh远程登录

ssh 192.168.1.100      # 默认利用当前宿主用户的用户名登录 ssh omd@192.168.1.100  # 利用远程机的用户登录 ssh omd@192.168.1.100  -o stricthostkeychecking=no # 首次登陆免输yes登录 ssh omd@192.168.1.100 "ls /home/omd"  # 当前服务器A远程登录服务器B后执行某个命令 ssh omd@192.168.1.100 -t "sh /home/omd/ftl.sh"  # 当前服务器A远程登录服务器B后执行某个脚本 复制代码

免密设置

  • 进入用户家目录中 .ssh 文件夹内

  • 根据DSA算法生成私钥和公钥

加密协议可以使用RSADSA,新版本的openssl 与 ssh-keygen中也可以使用更安全更快速的 ecdsa

ssh-keygen -t dsa / rsa     # 一路回车即可 -> id_dsa         -->私钥(钥匙)  -> id_dsa.pub     -->公钥(锁) 复制代码

  • id_dsa.pub的内容放入服务器.ssh目录下的authorized_keys文件中

  • 免密码登录目标服务器:

ssh username@servername -> ssh root@192.168.25.110 复制代码

配置文件

位置: /etc/ssh/sshd_config

# cat /etc/ssh/sshd_config  #       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $   # This is the sshd server system-wide configuration file.  See # sshd_config(5) for more information.   # This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin   # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented.  Uncommented options change a # default value.   #Port 22 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress ::   # Disable legacy (protocol version 1) support in the server for new # installations. In future the default will change to require explicit # activation of protocol 1 Protocol 2   # HostKey for protocol version 1 #HostKey /etc/ssh/ssh_host_key # HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_dsa_key   # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 1024   # Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO   # Authentication:   #LoginGraceTime 2m #PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #MaxSessions 10   #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile     .ssh/authorized_keys #AuthorizedKeysCommand none #AuthorizedKeysCommandRunAs nobody   # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts #RhostsRSAAuthentication no # similar for protocol version 2 #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # RhostsRSAAuthentication and HostbasedAuthentication #IgnoreUserKnownHosts no # Don't read the user's ~/.rhosts and ~/.shosts files #IgnoreRhosts yes   # To disable tunneled clear text passwords, change to no here! #PasswordAuthentication yes #PermitEmptyPasswords no PasswordAuthentication yes   # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes ChallengeResponseAuthentication no   # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no #KerberosUseKuserok yes   # GSSAPI options #GSSAPIAuthentication no GSSAPIAuthentication yes #GSSAPICleanupCredentials yes GSSAPICleanupCredentials yes #GSSAPIStrictAcceptorCheck yes #GSSAPIKeyExchange no   # 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 no UsePAM yes   # Accept locale-related environment variables AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS   #AllowAgentForwarding yes #AllowTcpForwarding yes #GatewayPorts no #X11Forwarding no X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes #PrintLastLog yes #TCPKeepAlive yes #UseLogin no #UsePrivilegeSeparation yes #PermitUserEnvironment no #Compression delayed #ClientAliveInterval 0 #ClientAliveCountMax 3 #ShowPatchLevel no #UseDNS yes #PidFile /var/run/sshd.pid #MaxStartups 10 #PermitTunnel no #ChrootDirectory none   # no default banner path #Banner none   # override default of no subsystems Subsystem       sftp    /usr/libexec/openssh/sftp-server   # Example of overriding settings on a per-user basis #Match User anoncvs #       X11Forwarding no #       AllowTcpForwarding no #       ForceCommand cvs server


作者:为为为什么
链接:https://juejin.cn/post/7034323282069307428


文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐