創建用戶
#創建用戶 useradd -s /sbin/nologin mysql useradd –d /usr/china -m china #設置密碼 passwd mysql passwd china #創建組 groupadd mysql #指定bash usermod -s /bin/bash mysql #幫助信息 useradd --help Usage: useradd [options] LOGIN Options: -b, --base-dir BASE_DIR 設置基本路徑作為用戶的登錄目錄 -c, --comment COMMENT 對用戶的注釋 -d, --home-dir HOME_DIR 設置用戶的登錄目錄 -D, --defaults 改變設置 -e, --expiredate EXPIRE_DATE 設置用戶的有效期 -f, --inactive INACTIVE 用戶過期后,讓密碼無效 -g, --gid GROUP 使用戶只屬於某個組 -G, --groups GROUPS 使用戶加入某個組 -h, --help 幫助 -k, --skel SKEL_DIR 指定其他的skel目錄 -K, --key KEY=VALUE 覆蓋 /etc/login.defs 配置文件 -m, --create-home 自動創建登錄目錄 -l, 不把用戶加入到lastlog文件中 -M, 不自動創建登錄目錄 -r, 建立系統賬號 -o, --non-unique 允許用戶擁有相同的UID -p, --password PASSWORD 為新用戶使用加密密碼 -s, --shell SHELL 登錄時候的shell -u, --uid UID 為新用戶指定一個UID
設置ssh登錄
用useradd新增的用戶不能直接用ssh遠程訪問,需要修改ssh相關配置如下:vi /etc/ssh/sshd_config 添加 AllowUsers 用戶
如下
Port 22 AddressFamily any ListenAddress 0.0.0.0 AllowUsers mysql china root #多個用戶用空格隔開 #ListenAddress :: # Disable legacy (protocol version 1) support in the server for new # installations. In future the default will change to require explicit # activation of protocol 1 Protocol 2 #HostKey for protocol version 1 #HostKey /etc/ssh/ssh_host_key #HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 1024 # Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO # Authentication: LoginGraceTime 120 PermitRootLogin yes # 允許root用戶登錄 StrictModes yes #MaxAuthTries 6 #MaxSessions 10 RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
修改完畢后,保存文件,重啟ssh服務
[root@china ~]# service sshd restart
停止 sshd: [確定]
正在啟動 sshd: [確定]