1.使用命令更新安裝列表:
sudo apt-get update
輸入當前用戶的管理員密碼回車即可。
2.使用apt-get
方式安裝ssh
sudo apt-get install openssh-server
輸入Y
回車即可。
3.查看是否安裝成功
sudo ps -ef |grep ssh
命令返回值中包含sshd
字樣即表明安裝成功并已開啟SSH服務,如下:
如果沒有出現該字樣,則嘗試使用
/etc/init.d/sshstart
或者sudo service ssh start
開啟SSH服務,然后再次使用sudo ps -ef|grep ssh
查看SSH服務是否開啟。
4.開啟、關閉、重啟SSH
開啟:sudo /etc/init.d/ssh start
關閉:sudo /etc/init.d/ssh stop
重啟:sudo /etc/init.d/ssh restart
5.配置root用戶支持SSH
SSH配置文件所在位置:/etc/ssh/sshd_config
使用sudo gedit /etc/ssh/sshd_config
命令打開配置文件:
1.在PermitRootLogin without-password
前加上'#',即注釋掉這句,如果文件中是PermitRootLogin prohibit-password
這句,則也是使用'#'注釋掉;
2.在1
中的那句下添加PermitRootLogin yes
。
修改后大致如下:
# Authentication:
LoginGraceTime 120
#PermitRootLogin without-password
PermitRootLogin yes
StrictModes yes
保存文件即可修改成功。
然后使用sudo /etc/init.d/ssh restart
命令重啟SSH服務,嘗試使用root
用戶使用ssh,即ssh root@10.10.10.10
(其中10.10.10.10即你的服務端IP地址)。輸入密碼后成功則會進入到服務端,并且用戶為root用戶。
輸入密碼后如果出現:
root@10.103.*.*'s password:
Permission denied, please try again.
則可能是需要配置root用戶密碼,則使用sudo passwd root
修改root用戶密碼,兩次輸入相同密碼即可。再次嘗試ssh root@10.10.10.10
進行連接。
6.ssh與scp的相關命令
1.登陸遠程主機:ssh username@192.168.1.1
,并輸入用戶密碼即可
2.從遠程主機復制文件到本地:
scp username@192.168.1.2:/home/fnl/a.txt /home/zhh/my_file_path/
(遠端主機IP為192.168.1.2,文件位置為/home/fnl/a.txt,復制到本地/home/zhh/my_file_path/)
3.從遠程主機復制文件夾到本地:
scp -r username@192.168.1.2:/home/fnl/your_file_path /home/zhh/my_file_path
(遠端主機IP為192.168.1.2,文件夾位置為/home/fnl/your_file_path,復制到本地/home/zhh/my_file_path)
4.從本地文件復制到遠端主機:
scp /home/zhh/my_file_path/a.txt username@192.168.1.2:/home/fnl/your_file_path/
(遠端主機IP為192.168.1.2,本地文件位置為/home/zhh/my_file_path/a.txt,復制到遠端/home/fnl/your_file_path/)
5.從本地主機復制文件夾到遠端主機:
scp -r /home/zhh/my_file_path username@192.168.1.2:/home/fnl/your_file_path
(遠端主機IP為192.168.1.2,本地文件夾位置為/home/zhh/my_file_path,復制到遠端/home/fnl/your_file_path)