1. 程式人生 > >Linux下SSH安裝、更新及XShell連線

Linux下SSH安裝、更新及XShell連線

本文以Ubuntu為例進行說明

ssh程式分為有客戶端程式openssh-client和服務端程式openssh-server。如果需要ssh登陸到別的電腦,需要安裝openssh-client,該程式ubuntu是預設安裝的。而如果需要從遠端連線到本機,則需要安裝openssh-server,該程式需要自己安裝。在Ubuntu下安裝openssh-server非常簡單。

首先,我們確認ssh服務是否已經安裝。

(1)輸入:ps -e|grep ssh,如果服務已經啟動,則可以同時看到‘ssh-agent’和‘sshd’。

a)如果只看到‘ssh-agent’則表示服務沒有啟動,需要啟動,輸入:

/etc/init.d/ssh start,如果輸入之後無反應或返回一些錯誤資訊,則表示ssh服務沒有安裝,需要安裝openssh-server。轉入(2)

b)如果同時看到ssh-agent’和‘sshd’,則表示服務正常,已啟動。

(2)輸入:sudo apt-get install openssh-server,安裝服務

如果出現

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
    等一些錯誤資訊,則表示你需要更新apt-get,命令:sudo apt-get update,當然此時你也許會碰到無法更新,因為國內網路限制,進行官方更新總會出各種問題,更新失敗時,你可以採用替換更新源的方法。步驟如下

1)先備份下系統原來的原地址列表檔案,命令:sudo cp /etc/apt/sources.list /etc/apt/sources.list.old

2)然後對該檔案進行修改,命令:sudo gedit/etc/apt/sources.list

可以在裡面新增資源地址,也可以直接覆蓋掉原來的。

以下有一些比較好的源,大家看情況新增兩三個即可。

#網易的源(163源,無論是不是教育網,速度都很快)
deb http://mirrors.163.com/ubuntu/ raring main universe restricted multiverse
deb-src http://mirrors.163.com/ubuntu/ raring main universe restricted multiverse
deb http://mirrors.163.com/ubuntu/ raring-security universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ raring-security universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ raring-updates universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ raring-proposed universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ raring-proposed universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ raring-backports universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ raring-backports universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ raring-updates universe main multiverse restricted

#搜狐的源(sohu 源今天還沒有更新,不過應該快了)
deb http://mirrors.sohu.com/ubuntu/ raring main restricted
deb-src http://mirrors.sohu.com/ubuntu/ raring main restricted
deb http://mirrors.sohu.com/ubuntu/ raring-updates main restricted
deb-src http://mirrors.sohu.com/ubuntu/ raring-updates main restricted
deb http://mirrors.sohu.com/ubuntu/ raring universe
deb-src http://mirrors.sohu.com/ubuntu/ raring universe
deb http://mirrors.sohu.com/ubuntu/ raring-updates universe
deb-src http://mirrors.sohu.com/ubuntu/ raring-updates universe
deb http://mirrors.sohu.com/ubuntu/ raring multiverse
deb-src http://mirrors.sohu.com/ubuntu/ raring multiverse
deb http://mirrors.sohu.com/ubuntu/ raring-updates multiverse
deb-src http://mirrors.sohu.com/ubuntu/ raring-updates multiverse
deb http://mirrors.sohu.com/ubuntu/ raring-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ raring-backports main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ raring-security main restricted
deb-src http://mirrors.sohu.com/ubuntu/ raring-security main restricted
deb http://mirrors.sohu.com/ubuntu/ raring-security universe
deb-src http://mirrors.sohu.com/ubuntu/ raring-security universe
deb http://mirrors.sohu.com/ubuntu/ raring-security multiverse
deb-src http://mirrors.sohu.com/ubuntu/ raring-security multiverse
deb http://extras.ubuntu.com/ubuntu raring main
deb-src http://extras.ubuntu.com/ubuntu raring main

#臺灣源(臺灣的ubuntu 更新源還是很給力的)
deb http://tw.archive.ubuntu.com/ubuntu/ raring main universe restricted multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ raring main universe restricted multiverse
deb http://tw.archive.ubuntu.com/ubuntu/ raring-security universe main multiverse restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ raring-security universe main multiverse restricted
deb http://tw.archive.ubuntu.com/ubuntu/ raring-updates universe main multiverse restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ raring-updates universe main multiverse restricted

#骨頭源,骨頭源是bones7456架設的一個Ubuntu源 ,提供ubuntu,deepin
deb http://ubuntu.srt.cn/ubuntu/ raring main universe restricted multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ raring main universe restricted multiverse
deb http://ubuntu.srt.cn/ubuntu/ raring-security universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ raring-security universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ raring-updates universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ raring-proposed universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ raring-proposed universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ raring-backports universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ raring-backports universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ raring-updates universe main multiverse restricted

#ubuntu.cn99.com源(推薦):
deb http://ubuntu.cn99.com/ubuntu/ raring main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ raring-updates main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ raring-security main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ raring-backports main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu-cn/ raring main restricted universe multiverse

#教育網源
#電子科技大學
deb http://ubuntu.uestc.edu.cn/ubuntu/ raring main restricted universe multiverse
deb http://ubuntu.uestc.edu.cn/ubuntu/ raring-backports main restricted universe multiverse
deb http://ubuntu.uestc.edu.cn/ubuntu/ raring-proposed main restricted universe multiverse
deb http://ubuntu.uestc.edu.cn/ubuntu/ raring-security main restricted universe multiverse
deb http://ubuntu.uestc.edu.cn/ubuntu/ raring-updates main restricted universe multiverse
deb-src http://ubuntu.uestc.edu.cn/ubuntu/ raring main restricted universe multiverse
deb-src http://ubuntu.uestc.edu.cn/ubuntu/ raring-backports main restricted universe multiverse
deb-src http://ubuntu.uestc.edu.cn/ubuntu/ raring-proposed main restricted universe multiverse
deb-src http://ubuntu.uestc.edu.cn/ubuntu/ raring-security main restricted universe multiverse
deb-src http://ubuntu.uestc.edu.cn/ubuntu/ raring-updates main restricted universe multiverse

#中國科技大學
deb http://debian.ustc.edu.cn/ubuntu/ raring main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ raring-backports restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ raring-proposed main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ raring-security main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ raring-updates main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ raring main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ raring-backports main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ raring-proposed main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ raring-security main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ raring-updates main restricted universe multiverse
#北京理工大學
deb http://mirror.bjtu.edu.cn/ubuntu/ raring main multiverse restricted universe
deb http://mirror.bjtu.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
deb http://mirror.bjtu.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
deb http://mirror.bjtu.edu.cn/ubuntu/ raring-security main multiverse restricted universe
deb http://mirror.bjtu.edu.cn/ubuntu/ raring-updates main multiverse restricted universe
deb-src http://mirror.bjtu.edu.cn/ubuntu/ raring main multiverse restricted universe
deb-src http://mirror.bjtu.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
deb-src http://mirror.bjtu.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
deb-src http://mirror.bjtu.edu.cn/ubuntu/ raring-security main multiverse restricted universe
deb-src http://mirror.bjtu.edu.cn/ubuntu/ raring-updates main multiverse restricted universe

#蘭州大學
deb ftp://mirror.lzu.edu.cn/ubuntu/ raring main multiverse restricted universe
deb ftp://mirror.lzu.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
deb ftp://mirror.lzu.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
deb ftp://mirror.lzu.edu.cn/ubuntu/ raring-security main multiverse restricted universe
deb ftp://mirror.lzu.edu.cn/ubuntu/ raring-updates main multiverse restricted universe
deb ftp://mirror.lzu.edu.cn/ubuntu-cn/ raring main multiverse restricted universe

#上海交通大學(上海交大源,教育網的速度不用說了)
deb http://ftp.sjtu.edu.cn/ubuntu/ raring main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ raring-security main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu/ raring-updates main multiverse restricted universe
deb http://ftp.sjtu.edu.cn/ubuntu-cn/ raring main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ raring main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ raring-security main multiverse restricted universe
deb-src http://ftp.sjtu.edu.cn/ubuntu/ raring-updates main multiverse restricted universe

    新增好後儲存,再輸入sudo apt-get update就可以更新了,慢慢等待下載吧。

(3)更新完之後,重新輸入sudo apt-get install openssh-server進行安裝,然後再輸入ps -e|grep ssh檢查是否啟動。

(4)啟動、停止和重啟openssh-server命令如下

/etc/init.d/ssh start /etc/init.d/ssh stop /etc/init.d/ssh restart
(5)配置openssh-server
openssh-server配置檔案位於/etc/ssh/sshd_config,在這裡可以配置SSH的服務埠等,例如:預設埠是22,可以自定義為其他埠號,如222,然後需要重啟SSH服務。
(6)Ubuntu中配置openssh-server開機自動啟動
開啟/etc/rc.local檔案,在exit 0語句前加入: /etc/init.d/ssh start

附帶一些基本命令,留存備用,傷不起的不經常使用Linux,忘得好快。

(1)Linux系統一般預設user使用者,此時要切換到root使用者時,命令:sudo su,預設時Ubuntu的root使用者是沒有固定密碼,密碼隨機產生並動態改變,不可直接用su(switch user)來切換

(2)root使用者切換回user使用者,命令:su user(user即你的使用者名稱),或直接輸入exitctrl+D

(3)給root設定一個密碼,命令:sudo passwd root,然後按提示來,此時即可利用su root,切換root使用者,或者sudo su

重啟命令:
1、reboot
2、shutdown -r now 立刻重啟(root使用者使用)
3、shutdown -r 10 過10分鐘自動重啟(root使用者使用)
4、shutdown -r 20:35 在時間為20:35時候重啟(root使用者使用)
如果是通過shutdown命令設定重啟的話,可以用shutdown -c命令取消重啟

關機命令:
1、halt 立刻關機
2、poweroff 立刻關機
3、shutdown -h now 立刻關機(root使用者使用)
4、shutdown -h 10 10分鐘後自動關機
如果是通過shutdown命令設定關機的話,可以用shutdown -c命令取消重啟