iterm2 安装、配置、ssh远程

简介: iterm2 安装、配置、ssh远程,持续更新。

iterm2 安装、配置、ssh远程

作为一名 IT 人员选择了 mac后,经常需要远程,以前用的是Xshell、CRT、MobaXterm,换了mac,尝试过很多免费的软件,最终使用下来还是觉得 Iterm2 方便,写个笔记,记录下自己配置过程,主要介绍下远程部分,后续有新发现在补充。

安装+基础配置

image.png

下载完后,安装很简单,直接解压双击后按照提示移动到应用即可。

image.png

设置为默认终端

打开安装后的iterm2 点击左上角的 iterm2,找到 Make iTerm2 Default Term 点击即可。

image.png

设置主题

下载后解压进入到schemes目录,该目录里有很多主题。

image.png

  • 导入主题

1、打开 iTerm2 点击左上角的 iterm2 -> settings

image.png

2、选择profiles ,使用默认profile或者新建一个profile,选择后依次点击 Colors -> Color Presets -> import

选择任意一个主题即可,我是用的是常用的 Solarized Dark Higher Contras,导入后,重新下拉选择这个主题即可。

image.png

image.png

安装 oh-my-zsh

打开 Iterm2 命令行执行命令即可

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • 修改默认shell
chsh -s /bin/zsh
  • 修改主题

可以查看下有哪些主题:https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

选择自己喜欢的主题样式,编辑配置文件修改主题样式即可。

vim ~/.zshrc

# 进入编辑模式后 输入/ZSH_THEME 回车 找到 ZSH_THEME="robbyrussell" 降 robbyrussell 替换成自己喜欢的主题即可。

image.png

配置快捷SSH远程

通过左上角 iterm2 -> settings -> profiles -> General 或者快捷键 command + ,

image.png

在 Command 选框中填入 /bin/zsh

在 Send text at start 中选择要远程连接的机器

如果连接的是堡垒机,连接的时候有以下问题的话

  • 报 Unable to negotiate with : no matching host key type found. Their offer: ssh-rsa
    • 解决 ssh 加上以下参数 ssh -oHostKeyAlgorithms=ssh-rsa XXX@XXX
  • 复制打开新窗口的时候发现还要二次输入验证码
    • 编辑 ~/.ssh/config
    • 在 profile 页面 Working Directory 选择 Reuse previous session‘s directory
vim ~/.ssh/config
#输入以下内容
host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
ForwardAgent yes

image.png

实现免密

本次介绍的是用户密码+ iterm2 profile 的形式,密钥的形式大家都会就不叙述了。

1、选择一个 profile 点击Advanced 找到 Triggers 点击 Edit

image.png

2、点击下面的 +

  • Regular Expression 填入 正则信息,第一个为 连接的时候输入yes/no的提示,该提示可以在命令行先ssh 一台机器看下提示复制粘贴过来即可。 第二个为提示密码部分,一般是ip's password:
  • Action 选择Send Text
  • Parameters输入参数,比如第一行填 yes\r \r 是回车的意思,第二行填密码以及\r
  • instant选项打上勾

image.png

然后测试即可。

可以先打开一个会话,如果正常登录,使用快捷键 Command+D 打开一个并列窗口测试是否正常。

安装配置lrzsz

rz、sz是将文件上传到远程服务器或者下载到本地的命令。但是 iterm2 本身不支持这两个命令,所以需要我们手动配置下,具体方式如下。

安装 lrzsz

如果没有安装 brew,需要先安装 brew, 如果已经安装但是显示没有这个命令需要在原生 iterm 查看命令路径,然后加到iterm2的环境变量中。具体操作如下:

  • 没有安装 brew安装
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • 已经安装但是显示没有brew
# 查看原生 iterm brew 路径
$ whereis brew
$ brew: /opt/homebrew/bin/brew /opt/homebrew/share/man/man1/brew.1

# 添加到zsh的环境变量中
$ vim ~/.zshrc
# 在最后一行添加

$ export PATH=/opt/homebrew/bin/:$PATH
  • 安装 lrzsz
$ brew install lrzsz

配置lrzsz

  • 新建脚本文件 iterm2-recv-zmodem.sh 内容如下,请注意脚本中 rz 命令的路径。
#!/bin/bash

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
    FILE=$(osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
else
    FILE=$(osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
fi

if [[ $FILE = "" ]]; then
    echo Cancelled.
    # Send ZModem cancel
    echo -e \\x18\\x18\\x18\\x18\\x18
    sleep 1
    echo
    echo \# Cancelled transfer
else
    cd "$FILE"
    /opt/homebrew/bin/rz -E -e -b --bufsize 4096
    sleep 1
    echo
    echo
    echo \# Sent \-\> $FILE
fi
  • 新建脚本文件 iterm2-send-zmodem.sh,内容如下,请注意脚本中 rz 命令的路径。
#!/bin/bash


osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
    FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
    FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
    echo Cancelled.
    # Send ZModem cancel
    echo -e \\x18\\x18\\x18\\x18\\x18
    sleep 1
    echo
    echo \# Cancelled transfer
else
    /opt/homebrew/bin/sz "$FILE" --escape --binary --bufsize 4096
    sleep 1
    echo
    echo \# Received $FILE
fi
  • 给两个文件执行权限
chmod +x /opt/homebrew/bin/iterm2-*
  • 配置profile,怎么打开 profile方式上文已介绍。打开 profile后,选择 Advanced,点击Edit,添加以下两个触发器。

​ 1、Regular Expression: receive.**B0100000

​ Action: Run Silent Coprocess

​ Parameters: /opt/homebrew/bin/iterm2-send-zmodem.sh

​ 2、Regular Expression: **B00000000000

​ Action: Run Silent Coprocess

​ Parameters: /opt/homebrew/bin/iterm2-recv-zmodem.sh

常用快捷键

Command + D 打开一个并列窗口

Shift + Command + D 打开一个并行窗口

Shift + Command + i多窗口同步操作

Option + Command + 方向键 切换窗口

Shift + Command + o 快速搜索选择profile,并打开基于该profile的会话

Command + , 快速打开 profile 界面

control + w 命令行删除一个单词

control + u 命令行快速删除到行首

control + k 命令行快速删除到行尾

control + d 命令行删除当前字母

相关文章
|
16天前
|
Ubuntu Shell 网络安全
安装了ubuntu虚拟机后发现shell无法连接 ubuntu开启ssh连接
【8月更文挑战第23天】安装了ubuntu虚拟机后发现shell无法连接
59 6
|
17天前
|
安全 网络协议 Shell
Github代码仓库SSH配置流程
这篇文章是关于如何配置SSH以安全地连接到GitHub代码仓库的详细指南,包括使用一键脚本简化配置过程、生成SSH密钥对、添加密钥到SSH代理、将公钥添加到GitHub账户以及测试SSH连接的步骤。
30 0
Github代码仓库SSH配置流程
|
23天前
|
网络安全 开发工具 git
拉取 gitee 代码,配置SSH,Please make sure you have the correct access rights
拉取 gitee 代码,配置SSH,Please make sure you have the correct access rights
29 1
|
23天前
|
Linux 网络安全 Python
Linux离线安装Python时ssh和hashlib死活安装不上的解决方案
本文提供了Linux环境下离线安装Python时遇到的"ImportError: No module named _ssl"和"ERROR:root:code for hash md5|sha1|sha224|sha256|sha384|sha512 was not found"两个问题的解决方案,通过设置OpenSSL环境变量和编辑Python源码配置文件来解决。
17 1
|
9天前
|
网络安全 Windows
在Windows电脑上启动并配置SSH服务
在Windows电脑上启动并配置SSH服务
25 0
|
10天前
|
网络安全 Windows
windows安装ssh服务
windows安装ssh服务
14 0
|
17天前
|
Ubuntu Shell 网络安全
【Ubuntu】配置SSH
【Ubuntu】配置SSH
26 0
|
17天前
|
安全 Linux 网络安全
在Linux中,如何配置SSH以确保远程连接的安全?
在Linux中,如何配置SSH以确保远程连接的安全?
|
3月前
|
安全 Linux Shell
Linux中SSH命令介绍
Linux中SSH命令介绍