1. 环境准备
- 下载镜像
下载ubuntu-18.04.4-live-server-amd64.iso
http://mirrors.aliyun.com/ubuntu-releases/18.04/ubuntu-18.04.4-live-server-amd64.iso - 配置虚拟机资源
内存 | 处理器数量 | 处理器核数 | 硬盘 |
---|---|---|---|
2G | 2 | 1 | 20G |
- vmvare 选择镜像
2.安装
整体按照提示,直接安装即可
STEP1
STEP2
STEP3: 此处注意将keyboard相关设置调整为Chinese
STEP4
STEP5
STEP6
STEP7
STEP8
STEP9
STEP10
STEP11
STEP12
STEP13
STEP14
STEP15
3 基础配置
3.1 root免密设置
dd@ubuntu04:~$ sudo passwd root
[sudo] passwd for dd:
Enter new UNIX password:
Retype new UNIX password:
passwd: password update successfully
3.2 ssh安装
a. 检查ssh
Ubuntu系统默认是没有SSH服务的,故要检查SSH服务是否已安装。
打开终端输入以下指令:
dd@ubuntu04:~$ ps -e | grep ssh
1796 ? 00:00:00 sshd
2003 ? 00:00:00 sshd
2082 ? 00:00:00 sshd
2215 ? 00:00:00 sshd
2282 ? 00:00:00 sshd
若输入指令后显示类似于上图所示,则说明SSH服务已启动
其中sshd表示ssh-server已启动,ssh表示ssh-client已启动
b. 安装SSH服务
sudo apt-get install openssh-client
sudo apt-get install openssh-server
c. 启动SSH服务
sudo /etc/init.d/ssh start
启动后通过以下指令判断SSH服务是否正确启动:
ps -e | grep ssh
3.3 修改ip地址
修改前:
dd@ubuntu04:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.132 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::20c:29ff:fed0:84b3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d0:84:b3 txqueuelen 1000 (Ethernet)
RX packets 1270 bytes 789800 (789.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 466 bytes 41222 (41.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 105 bytes 8287 (8.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 105 bytes 8287 (8.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
进行修改:
dd@ubuntu04:~$ sudo vim /etc/netplan/50-cloud-init.yaml
[sudo] password for dd:
dd@ubuntu04:~$ sudo vim /etc/netplan/50-cloud-init.yaml
dd@ubuntu04:~$ sudo netplan apply
修改内容:
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
ens33:
addresses:
- 192.168.31.204/24
gateway4: 192.168.31.2
nameservers:
addresses:
- 192.168.31.2
search: []
dhcp4: no
optional: true
version: 2
修改之后:
dd@ubuntu04:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.204 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::20c:29ff:fed0:84b3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d0:84:b3 txqueuelen 1000 (Ethernet)
RX packets 2121 bytes 865084 (865.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 974 bytes 133551 (133.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 105 bytes 8287 (8.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 105 bytes 8287 (8.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
TIPS:
相信很多人在vim里粘贴带有缩进的内容都会遇到缩进混乱的销魂效果(如下图所示)这个其实不是什么 bug。而是自动缩进autoindent搞的鬼,那我们也不能关了自动缩进。解决方法如下,在粘贴前输入下面的命令:
:set paste
然后按i切换为输入模式,你会发现最下方显示就会显示:
—INSERT—(paste)
这样你再粘贴的内容就是正常的了。粘贴完成之后记得切换为命令模式输入下面的命令来退出粘贴模式,不然你就会发现自动缩进失效了。
:set nopaste
3.4 sudo添加
sudo vim /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
dd ALL=(ALL:ALL) NOPASSWD:ALL # add
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
%sudo ALL=(ALL:ALL) NOPASSWD:ALL # add
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
3.5 ssh免密登录
dd@ubuntu05:~$ sudo vim /etc/hosts
配置hosts
127.0.0.1 localhost
127.0.1.1 ubuntu05
192.168.31.204 ubuntu04
192.168.31.205 ubuntu05
192.168.31.206 ubuntu06
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
生成秘钥
dd@ubuntu05:~$ ssh-keygen
拷贝秘钥
ssh-copy-id ubuntu05
ssh-copy-id ubuntu06
3.6 scp 配置
scp出现Permission denied (publickey). lost connection
原因:scp是基于ssh的拷贝服务,ssh在没有密钥登录的情况下,禁用了密码登录。想要解决只需要修改配置文件。
登录远程主机,将/etc/ssh/sshd_config文件中的PasswordAuthentication no 改为PasswordAuthentication yes
重启sshd服务
systemctl restart ssh.service
4 参考文献
[1] Ubuntu18.04设置sudo免密码
[2] Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输
[3] Ubuntu 18.04修改IP地址
[4] Ubuntu18.04 首次使用设置 root 密码
[5] Ubuntu18.04 修改IP地址、查看网关、防火墙