Useful Commands in Linux

When you install a Linux server or PC, you need to configure the system at first. Here is a collection of useful commands taking Ubuntu as example.

Common Commands

Change Directory (cd)

cd /home/Qjbtiger/files # change directory to path
cd .\ # cd to current directory
cd ..\ # cd to parent directory
cd \ # cd to root directory
cd ~ # cd to current user's home directory

List the Files(ls)

ls /home/Qjbtiger/files # list all files in path '/home/Qjbtiger/files'
ls ./ # list all files in current path
ls # same as above
ls -l # list all files in details
ls -a # list all files including hidden files

Make Directory (mkdir)

mkdir /home/Qjbtiger/files/newDirectory # make directory named 'newDirectory' in path '/home/Qjbtiger/files'
mkdir ./newDirectory # make directory named 'newDirectory' in current path
mkdir newDirectory # same as above

Delete Files or Directories(rm)

rm /home/Qjbtiger/files/fileReadyToDelete.txt # delete the file named 'fileReadyToDelete' in path '/home/Qjbtiger/files'
rm ./fileReadyToDelete.txt # delete the file named 'fileReadyToDelete' in current path
rm ./* # delete all files in current path. Command 'rm' support for wildcard character
rm -r /home/Qjbtiger/files/DirectoryReadyToDelete # delete the directory named 'DirectoryReadyToDelete' in path '/home/Qjbtiger/files'. '-r' means deleting directories and their contents recursively
rm -rf /* # ready to run away~~

Compression & Decompression

tar -cf /home/Qjbtiger/tmp.tar /home/Qjbtiger/tmp.txt # create new archive (no compression) and '-f' is usually necessary
tar -zcf /home/Qjbtiger/tmp.tar.gz /home/Qjbtiger/tmp.txt # create new archive (with gzip compression)
tar -jcf /home/Qjbtiger/tmp.tar.bz2 /home/Qjbtiger/tmp.txt # create new archive (with bzip2 compression)
tar -xf /home/Qjbtiger/tmp.tar # extract the archive (no compression)
tar -zxf /home/Qjbtiger/tmp.tar.gz # extract the archive (with gzip compression)
tar -jxf /home/Qjbtiger/tmp.tar.bz2 # extract the archive (with bzip2 compression)
tar -zvxf /home/Qjbtiger/tmp.tar.gz # with verbose

zip /home/Qjbtiger/tmp.zip /home/Qjbtiger/tmp.txt # compress files
zip -r /home/Qjbtiger/tmp.zip /home/Qjbtiger/tmp # compress directory
unzip /home/Qjbtiger/tmp.zip # decompress the archive

Network Related

Check ip Address (ipconfig)
ifconfig
Show Network Status (netstat)
netstat # show active connections
netstat -a # show all ports including Listened and not listened
netstat -at # show all port of tcp links
netstat -au # show all port of udp links
netstat -l # show all listened ports
netstat -s # show statustic information
netstat -p # show PID and process name
netstat -n # show ip adress instead of hosts/ports/users name

netstat -tulpn # usually use

System Service (systemctl)

systemctl start smbd # start service 'smbd'
systemctl stop smbd # stop service 'smbd'
systemctl restart smbd # restart service 'smbd'
systemctl reload smbd # reload config file of service 'smbd'
systemctl status smbd # start service 'smbd'
systemctl enable smbd # start service 'smbd' when starting up
systemctl disable smbd # don't start service 'smbd' when starting up

User Management

Add (New) User

Make sure you do it by Root user or add sudo before the commands below.

adduser newUserName
adduser temp
Adding user `temp' ...
Adding new group `temp' (1001) ...
Adding new user `temp' (1001) with group `temp' ...
Creating home directory `/home/temp' ...
Copying files from `/etc/skel' ...
New password: # input password
Retype new password: 
passwd: password updated successfully
Changing the user information for temp
Enter the new value, or press ENTER for the default
        Full Name []: # input information or press 'Enter' to leave a blank
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] Y # input 'Y'

Delete User

Make sure you do it by Root user or add sudo before the commands below.

userdel deleteUseName # delete the user named 'deleteUseName' but its home directory won't be delete.
userdel -r deleteUserName # delete the user named 'deleteUseName' and its home directory will be delete as well.

Setting SSH Transport

Add Key

# generate key
ssh-keygen # input command 1
Generating public/private rsa key pair.
Enter file in which the key is (/home/qjbtiger/.ssh/id_rsa): # just press 'Enter'
Created directory '/home/qjbtiger/.ssh'
Enter passphrase (empty for no passphrase): # input passphrase or press 'Enter' to leave a blank 
Enter same passphrase again: # same as above
Your identification has been saved in /home/qjbtiger/.ssh/id_rsa.
Your public key has been saved in /home/qjbtiger/.ssh/id_rsa.pub

# install public key
cd ~/.ssh # input command 2 & 3
cat id_rsa.pub >> authorized_keys

# optional step
chmod 600 authorized_keys
chmod 700 ~/.ssh

Make sure you do it by Root user or add sudo before the commands below.

# setting ssh config file
vi /etc/ssh/sshd_config # input command 4

RSAAuthentication yes # enable RSA authentication
PubkeyAuthentication yes # allow public key authentication
PermitRootLogin yes # allow Root user log in
PasswordAuthentication yes # allow log in by password

# restart ssh service
service sshd restart # input command 5

Download the private key file ‘id_rsa’ to local PC. If you use PuTTY or some tools like this, you need to convert the file to new format by PuTTYGen which PuTTY can recognize.

Setting sudo Command

Make sure you do it by Root user or add sudo before the commands below.

vi /etc/sudoers
# User privilege specification
root	ALL=(ALL:ALL) ALL
qjbtiger ALL=(ALL:ALL) ALL
#----Add the user here! The first ALL means all hosts. The second ALL means all users can be used to exxcute commands. The third ALL means all groups can be used to exxcute commands. The fourth ALL means ALL commands can be executed----#

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL
#----Add the group here!----#

Setting SFTP (vsftpd)

First Setting

If necessary, it is recommanded to do it by Root user or add sudo before the commands below, unless only one local user uses it.

# optional step
apt-get update
apt-get upgrade

# install vsftpd
apt-get install vsftpd # input command 1
# config SFTP config file
vi /etc/vsftpd.conf # input command 2

anonymous_enable=NO # forbid anonymous user log in
local_enable=YES # IMPORTANT. allow loacl user log in
write_enable=YES # allow write (or delete) files


# chroot (optional)
chroot_local_user=YES # restrict local users to their home directories (Root directory is not allowed)
allow_writeable_chroot=YES # allow to write in root directory
chroot_list_enable=YES # users in  files is forbidden to change Root directory
chroot_list_file=/etc/vsftpd/chroot_list # file path in '/etc/vsftpd/chroot_list'


# userlist (optional)
userlist_enable=YES
userlist_file=/etc/vsftpd/user_list # add users alled to log in path '/etc/vsftpd.user_list'
userlist_deny=NO

# optional
local_root=/home/$USER # the directory when user log in, default is '/home/$USER'
# restart vsftpd service
service vsftpd restart # input command 3

Common Commands

service vsftpd start # start
service vsftpd stop # stop
service vsftpd status # check status
service vsftpd restart # restart

Other

Remerber to allow port 20/21 and 30000:31000 when configuring firewall. If necessary, you can new a user specific to SFTP

Setting Samba Server (samba)

ATTENTION: The default port 139/445, which is used by smb service, is forbidden by broadband operator in China, and the clients of Windows is not support for changing ports of smb service. So it is recommanded to use it in LAN instead of public network.

Make sure you do it by Root user or add sudo before the commands below.

First Setting

# optional step
apt-get update
apt-get upgrade

# install samba
apt-get install samba # input command 1
# edit config file
vi /etc/samba/smb.conf # input command 2

# add those words at the end of config file
[share]
  path = /home/qjbtiger
  valid users = qjbtiger # the user name you allow
  available = yes
  browseable = yes
  public = no # need password to log in
  writable = yes
# check config file is correct or not (optional)
testparm

# add share user
smbpasswd -a qjbtiger # input command 3
New SMB password: # input password
Retype new SMB password:
Added user qjbtiger.

# restart smb service
systemctl reload smbd nmbd # input command 4

Common Commands

systemctl start smbd nmbd # start samba
systemctl stop smbd nmbd # stop samba
systemctl restart smbd nmbd # restart samba
systemctl reload smbd nmbd # reload config file

systemctl status smbd nmbd # check samba status
smbstatus # same as above

Other

Remerber to allow port 139/445 when configuring firewall. If necessary, you can new a user specific to samba.

Or,

ufw allow 'Samba'

Setting Firewall (ufw)

Make sure you do it by Root user or add sudo before the commands below.

ufw enable # start firewall
ufw disable # stop firewall
ufw reload # restart firewall
ufw status # check status
ufw allow 22 # allow port 22
ufw delete allow 22 # forbid port 22 (delete the rule above)

My blog: https://qjbtiger.github.io/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值