ssh常用命令50条

SSH(Secure Shell)是一种用于远程登录、数据传输和命令执行的安全协议。下面列举了 SSH 命令的一些常见用法:

  1. 连接到远程主机:
ssh username@hostname

其中 username 是您要连接的远程主机的用户名,hostname 是远程主机的 IP 地址或主机名。如果您使用默认的 SSH 端口(22),则可以省略 -p 参数。

  1. 指定非默认端口号连接:
ssh -p port_number username@hostname

其中 port_number 是您要连接的远程主机上的 SSH 服务所在的端口号。

  1. 使用公钥身份验证连接到远程主机:
ssh -i private_key_file username@hostname

其中 private_key_file 是您的私钥文件的路径。

  1. 在连接时压缩流量:
ssh -C username@hostname

这可以在带宽有限的网络环境中提高效率。

  1. 将本地端口转发到远程主机:
ssh -L local_port:remote_host:remote_port username@hostname

其中 local_port 是您要监听的本地端口号,remote_hostremote_port 是您要连接到的远程主机和端口号。

  1. 将远程主机端口转发到本地主机:
ssh -R remote_port:local_host:local_port username@hostname

其中 remote_port 是您要监听的远程端口号,local_hostlocal_port 是您要连接到的本地主机和端口号。

  1. 在连接中执行单个命令:
ssh username@hostname command

其中 command 是您要在远程主机上执行的命令。

  1. 退出 SSH 连接:
exit
  1. 复制本地文件到远程主机:
scp file_name username@hostname:/remote/directory

其中 file_name 是本地文件的路径,/remote/directory 是远程主机上您要复制文件的目标目录。

  1. 从远程主机复制文件到本地:
scp username@hostname:/remote/file_path local_path

其中 remote/file_path 是远程主机上的文件路径,local_path 是本地计算机上的目标路径。

  1. 使用 tar 命令在远程主机上打包文件:
ssh username@hostname "tar -czvf archive.tar.gz /path/to/directory"
  1. 在本地计算机上解压缩远程主机上打包的文件:
scp username@hostname:/remote/archive.tar.gz /local/directory
cd /local/directory
tar -xzvf archive.tar.gz
  1. 显示当前用户的公钥信息:
ssh-add -l
  1. 将新的私钥添加到 SSH 代理:
ssh-add path/to/private_key_file
  1. 从 SSH 代理中删除私钥:
ssh-add -d path/to/private_key_file
  1. 显示当前用户的 IP 地址:
curl ifconfig.me
  1. 显示远程主机上可用 SSH 版本:
ssh -v username@hostname
  1. 连接到远程主机并启动远程图形界面程序:
ssh -X username@hostname program_name

其中 program_name 是您要在远程主机上启动的图形界面程序的名称。

  1. 使用 rsync 命令将本地目录同步到远程主机:
rsync -a /local/directory/ username@hostname:/remote/directory/
  1. 显示远程主机上正在运行的进程列表:
ssh username@hostname ps aux
  1. 使用 rsync 命令将远程主机上的目录同步到本地:
rsync -a username@hostname:/remote/directory/ /local/directory/
  1. 连接到远程主机并映射远程端口:
ssh -L local_port:remote_service:remote_port username@hostname

其中 local_port 是您在本地计算机上要监听的端口号,remote_service 是您希望连接到的远程服务的名称,remote_port 是远程主机上该服务所侦听的端口号。

  1. 列出远程主机上的所有用户:
ssh username@hostname getent passwd
  1. 在远程主机上创建新用户:
ssh username@hostname sudo adduser new_user_name
  1. 在远程主机上删除用户:
ssh username@hostname sudo deluser user_name
  1. 在远程主机上修改用户密码:
ssh username@hostname sudo passwd user_name
  1. 在远程主机上显示系统信息:
ssh username@hostname uname -a
  1. 将本地计算机上的文件夹复制到远程主机:
scp -r local_directory username@hostname:/remote/directory
  1. 在远程主机上安装软件包:
ssh username@hostname sudo apt-get install package_name
  1. 从远程主机上卸载软件包:
ssh username@hostname sudo apt-get remove package_name
  1. 在远程主机上更新软件包:
ssh username@hostname sudo apt-get update
  1. 在远程主机上升级软件包:
ssh username@hostname sudo apt-get upgrade
  1. 连接到远程主机并使用 root 用户身份运行命令:
ssh username@hostname sudo command

其中 command 是您要在远程主机上以 root 权限执行的命令。

  1. 使用 SSH 连接到远程主机,并将本地文件重定向到远程主机上的文件:
cat local_file | ssh username@hostname "cat > remote_file"

其中 local_file 是您的本地文件路径,remote_file 是您要在远程主机上创建或覆盖的文件路径。

  1. 在远程主机上显示计算机的连接历史记录:
history
  1. 在远程主机上创建新的目录:
ssh username@hostname mkdir directory_name

其中 directory_name 是您要在远程主机上创建的目录名称。

  1. 在远程主机上删除目录:
ssh username@hostname rmdir directory_name
  1. 在远程主机上复制目录:
ssh username@hostname cp -r source_directory target_directory

其中 source_directory 是您要复制的目录,target_directory 是您要将该目录复制到的目标目录。

  1. 在远程主机上移动或重命名文件或目录:
ssh username@hostname mv old_file_or_directory new_file_or_directory

其中 old_file_or_directory 是您要重命名或移动的现有文件或目录,new_file_or_directory 是该文件或目录的新名称或目标位置。

  1. 在远程主机上查找特定文件或目录:
ssh username@hostname find /path/to/directory -name "filename.txt"

其中 /path/to/directory 是您要查找文件的起始目录,filename.txt 是要查找的文件名称。

  1. 在远程主机上显示所有已安装的软件包:
ssh username@hostname dpkg --list
  1. 在远程主机上显示系统的 CPU 信息:
ssh username@hostname cat /proc/cpuinfo
  1. 在远程主机上显示系统的内存信息:
ssh username@hostname cat /proc/meminfo
  1. 在远程主机上显示系统中正在运行的服务信息:
ssh username@hostname systemctl list-units --type service
  1. 在远程主机上显示当前用户的登录历史记录:
ssh username@hostname last
  1. 在远程主机上显示所有可用的网络接口:
ssh username@hostname ip link show
  1. 在远程主机上显示所有已安装的磁盘和文件系统:
ssh username@hostname lsblk
  1. 在远程主机上显示已安装的 PCI 设备列表:
ssh username@hostname lspci
  1. 在远程主机上显示已安装的 USB 设备列表:
ssh username@hostname lsusb
  1. 在远程主机上显示所有已安装的网络设备:
ssh username@hostname ifconfig -a

这些命令可以根据实际需要来使用,以完成具体任务。

猜你喜欢

转载自blog.csdn.net/m0_55877125/article/details/130215631
今日推荐