Ubuntu安装完毕后,一般只有ssh客户端,使用 ps -ef |grep ssh 也可以发现只有ssh相关的进程。而我们需要用xshell终端通过SSH登录到安装好的Ubuntu时,就需要Ubuntu支持SSH服务端(sshd),并开启sshd服务。所以首先下载并安装ssh服务端(sshd)。
通过apt-cache search可查询到sshd包含在 openssh-server 软件包中,因此我们只需要安装 openssh-server 即可。
xxxx@~$ apt-cache search sshd
openssh-server - secure shell (SSH) server, for secure access from remote machines
fail2ban - ban hosts that cause multiple authentication errors
libconfig-model-cursesui-perl - curses interface to edit config data through Config::Model
libconfig-model-openssh-perl - configuration editor for OpenSsh
libconfig-model-tkui-perl - Tk GUI to edit config data through Config::Model
python-logsparser - Python library for log parsing, tagging and analysis.
xxxx@~$
xxxx@~$ sudo apt-get install openssh-server
安装完毕后,重启ssh服务(service sshd restart),通过 ps -ef |grep sshd 就可以看到sshd服务已经运行起来了。此时我们就可以在xshell终端通过ssh登录到Ubuntu了,默认ssh账户和密码为Ubuntu的登录账户及密码。
由于刚刚的默认ssh账户目录(/home/xxxx)下有很多其他的文件及目录:examples.desktop 公共的 模板 视频 图片 文档 下载 音乐 桌面
所以我们希望新建一个ssh账户aaaa,这样会自动生成/home/aaaa目录,此时该目录下就比较干净了。下面开始新建ssh账户:
xxxx@~$ sudo adduser aaaa
正在添加用户"aaaa"...
正在添加新组"aaaa" (1001)...
正在添加新用户"aaaa" (1001) 到组"aaaa"...
创建主目录"/home/aaaa"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码:
重新输入新的 UNIX 密码:
passwd:已成功更新密码
正在改变 aaaa的用户信息
请输入新值,或直接敲回车键以使用默认值
全名 []:
房间号码 []:
工作电话 []:
家庭电话 []:
其它 []:
这些信息是否正确? [Y/n] y
重启ssh服务并登录到这个aaaa账户后,我们发现无法使用超级账户:
aaaa@xx:~$ sudo vim .bashrc
[sudo] aaaa 的密码:
aaaa 不在 sudoers 文件中。此事将被报告。
所以还需要为aaaa账户添加权限。
使用之前的默认账户,修改这个sudoers文件内的权限,默认为440(只读)
sudo chmod 740 /etc/sudoers
sudo vim /etc/sudoers
找到# Allow members of group sudo to execute any command
%sudo ALL=(ALL) ALL
在下面添加一行,如下
aaaa ALL=(ALL) ALL (此处的aaaa就是刚刚新建的ssh用户名!)
随后重启ssh服务就可以了,为了安全,记得将sudoers文件的权限还原回去
sudo chmod 440 /etc/sudoers
随后测试,在不使用超级用户(sudo)的前提下,使用aaaa账户登录后,只能在/home/aaaa及其子目录下面创建或删除文件及文件夹,去/home目录或者/home/xxxx目录,就无法创建或删除,必须加sudo才可以。当然,xxxx用户也只能在/home/xxxx及其子目录下使用它的权限,取其他目录就需要加sudo。