linux --- 控制服务以及远程连接

控制服务

1.用什么控制服务
	系统初始化进程可以对服务进行相应的控制
2.当前系统初始化进程是什么
	systemd		---> 系统初始化进程
	pstree		---> 显示系统中的进程树
3.进程控制命令
	ssh 	    -------->   sshd
	client(客户端口)	      server(服务端口)
4.systemctl -------->服务控制命令
	systemctl 	status	sshd	##查看服务状态,inactive(不可用),active(可用)
	systemctl	start 	sshd	##开启服务
	systemctl	stop 	sshd	##关闭服务
	systemctl 	restart	sshd	##重启服务
	systemctl 	reload	sshd	##重新加载服务配置
	systemctl 	enable	sshd	##设定服务开机启动
	systemctl	disable	sshd	##设定服务开机不启动
	systemctl	list-units	##列出已经开启服务当前状态
	systemctl 	list-unit-files	##列出所有服务开机启动的状态 disable,enable,static
	systemctl 	list-dependencies	##列出服务的倚赖
	systemctl	set-default multi-user.target	##设定系统启动级别为多用户模式(无图形)
	systemctl	set-default graphical.target	##设定系统启动级别为图形模式

远程连接(需要密码登陆)

ssh 远程链接

ssh 对方用户名@对方主机ip地址  ---> 远程链接虚拟机
1.nm-connection-editor ---> 图形界面更改ip地址
2.ip 地址查看 
	ip addr show  ----> 建议使用
	ifconfig 		---> 不建议使用
3.连接方式
	ssh	username@ip	文本模式的连接
	ssh	-X username@ip	可以在连接成功后打开图形
	cheese			 ---> 连接开启对方电脑的摄像头
注意:第一次连接陌生主机时要建立认证文件;会询问是否建立连接,需要输入yes;下次不再需要
      再次连接此主机时,因为已经生成~/.ssh/know_hosts
4.远程复制
	scp	file	username@ip:dir		#上传(dir为绝对路径)
	scp	username@ip:file	dir	#下载(file为绝对路径)
	scp	-rp	username@ip:dir	dir	#下载(dir为绝对路径)

远程连接(不需要密码登陆)

ssh的key认证

#client (客户端口)
[root@hao ~]# ssh-keygen  ---> 生成密钥的命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #指定保存加密字符的文件
Enter passphrase (empty for no passphrase):   ---> 设定密码
Enter same passphrase again:    ----> 在此确定设定的密码
Your identification has been saved in /root/.ssh/id_rsa.    ---> 私钥
Your public key has been saved in /root/.ssh/id_rsa.pub.    ---> 公钥
The key fingerprint is:
e4:94:45:27:62:b9:94:66:42:87:09:6f:2d:67:2d:b4 root@hao
The key's randomart image is:
+--[ RSA 2048]----+
|    .o.o=++ .    |
|     .+=** o     |
|      +=E..      |
|     . B..       |
|        S        |
|                 |
|                 |
|                 |
|                 |
+-----------------+
[root@hao ~]# cd /root/.ssh/
[root@hao .ssh]# ls  	  ---> 查看密钥是否生成
id_rsa #(私钥) id_rsa.pub #(公钥) known_hosts 
[root@hao .ssh]# ssh-copy-id -i id_rsa.pub [email protected]   --->  将公钥给主机ip为113的root用户,加密root用户
The authenticity of host '172.25.254.113 (172.25.254.113)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? yes #
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.


[root@hao .ssh]# ls
authorized_keys #(这个文件出现,表示加密成功) id_rsa  id_rsa.pub  known_hosts
[root@hao .ssh]# scp id_rsa [email protected]:/root/.ssh/ #分发钥匙,将主机ip为113的私钥分发给ip为213的主机
[email protected]'s password: 
id_rsa                                        100% 1675     1.6KB/s   00:00    

#server(服务端口)
[root@yu ~]# cd /root/.ssh/
[root@yu .ssh]# \ls
id_rsa	known_hosts
[root@yu .ssh]# ssh [email protected] #连接时发现直接登陆不需要输入ip113 root密码
Last login: Wed Oct  3 00:25:55 2018 from 172.25.254.13
[root@hao ~]# logout
Connection to 172.25.254.113 closed.
#修改主机名称更改
[kiosk@foundation13 ~]$ hostnamectl set-hostname hao  #更改主机名称为hao
[kiosk@foundation13 ~]$ hostname		      #查看主机名称

猜你喜欢

转载自blog.csdn.net/hzyuhz/article/details/83011385