Passa al contenuto principale

Linux Commands 2

1. Login with SSH

  1. SSH can be used to login to a remote system, execute commands remotely, and more.

    Assuming that we have the account user1 on the server 10.92.186.210, we can login to it like this:

    ssh user1@10.92.186.210
    ls -al
    exit

    We can also use ssh to just run a command remotely:

    ssh user1@10.92.186.210 ls -al
    ssh user1@10.92.186.210 whoami
    ssh user1@10.92.186.210 ls .*
    ssh user1@10.92.186.210 'ls .*'
  2. Writing a password each time that we use ssh quickly becomes tedious. We can use keys instead, which is easier and more secure.

    First let's generate a public/private key pair:

    ssh-keygen --help
    ssh-keygen -t ecdsa -f key1
    ls -al key*
    cat key1
    cat key1.pub

    In order to be able to login to the server with this key, we need to send the public part of it to the server:

    ssh-copy-id -i key1.pub user1@10.92.186.210

    Now let's try to login using the private key as an identity file:

    ssh -i key1 user1@10.92.186.210

    ls -al
    ls -al .ssh/
    cat .ssh/authorized_keys
    exit

    cat key1.pub

    You may notice that the public key has been appended to .ssh/authorized_keys on the server.

  3. It gets even better. Let's create an SSH config file:

    mkdir -p ~/.ssh
    chmod 700 ~/.ssh/
    mv key1 ~/.ssh/
    ls -al ~/.ssh/

    # nano ~/.ssh/config
    cat <<EOF >> ~/.ssh/config
    Host server1
    HostName 10.92.186.210
    User user1
    IdentityFile ~/.ssh/key1
    EOF

    cat ~/.ssh/config

    Now we can just use ssh with the name server1, without having to specify the hostname (or IP) of the server, the username, the identity file etc. It will get them automatically from the config file.

    ssh server1
    exit
    ssh server1 ls -al

2. scp and sftp

These tools use an SSH tunnel for a secure communication with the server. Now that we have an easy ssh access to the server, we can also use easily these tools:

touch foo.txt
scp foo.txt server1:
ssh server1 ls -l

ssh server1 touch bar.txt
ssh server1 ls -l
scp server1:bar.txt .
ls -l bar.txt
sftp server1
help
ls
lls
get bar.txt
put foo.txt
quit

3. Archiving

# create test dir
mkdir -p testdir/dir-{001..100}
touch testdir/dir-{001..100}/file-{A..Z}
ls testdir/
ls testdir/dir-001/
# create archive
tar -c -f testdir.tar testdir
tar -cf testdir.tar testdir
ls -lh
# list content
tar -tf testdir.tar | less
tar -tvf testdir.tar | less
# extract
mkdir foo
cd foo
tar -xf ../testdir.tar
ls
tree -C | less -r
cd ..
rm -rf foo/
# use the options z or j to compress the archive:
tar -czf testdir.tgz testdir
ls -lh
tar -cjf testdir.tbz testdir
ls -lh

tar -tvzf testdir.tgz | less
tar -tvjf testdir.tbz | less

mkdir foo
cd foo
tar -xzf ../testdir.tgz
ls
cd ..
rm -rf foo

mkdir foo
cd foo
tar -xjf ../testdir.tbz
ls
cd ..
rm -rf foo

4. rsync

# local rsync
rsync -av testdir foo
ls foo
rsync -av testdir foo

# rsync with a remote server
rsync -av testdir server1:
ssh server1 ls
ssh server1 ls testdir
rsync -av testdir server1:

touch testdir/dir-099/file-Z
rsync -av testdir server1:

# deleting
rm testdir/dir-099/file-Z
rsync -av testdir server1:
ssh server1 ls testdir/dir-099/file-Z
rsync -av --delete testdir server1:
ssh server1 ls testdir/dir-099/file-Z

5. Adding user accounts

ssh server1

useradd --help
useradd -m -s /bin/bash test1
sudo useradd -m -s /bin/bash test1

ls /home/
ls -al /home/test1/
sudo ls -al /home/test1/

sudo passwd test1

sudo su -l test1
pwd
whoami
id
exit

pwd
whoami
id

ls -l /etc/passwd
file /etc/passwd
less /etc/passwd

ls -l /etc/group
file /etc/group
less /etc/group

ls -l /etc/shadow
file /etc/shadow
less /etc/shadow
sudo less /etc/shadow

# change the owner of a file
touch foo.txt
chown root: foo.txt
whoami
sudo chown root: foo.txt
ls -l foo.txt
chown test1:root foo.txt
ls -l foo.txt

6. The environmet

Environment Variables are maintained by the shell and are used to store some settings. They can also be used by some programs to get configuration values.

  1. We can display a list of environment variables with printenv or set:

    printenv | less
    set | less

    echo $USER
    echo $HOME
    echo $PATH
    echo $PW1

    PATH is used by shell to find a program. For example when we call ls, shell is looking for it in the first directory of the PATH, then in the second, and so on. The command which ls shows us where the shell finds the program ls.

  2. The environment variables are declared in some configuration files that the shell loads when it starts. There are two kinds of shells: a login shell session, which is started when we are prompted for a username and password, and a non-login shell session, which is started when we launch a terminal.

    # config scripts loaded by a login shell
    nano /etc/profile
    nano ~/.profile

    # config scripts loaded by a non-login shell
    nano /etc/bash.bashrc
    nano ~/.bashrc
  3. Let's say that we want to modify the variable HISTSIZE, which keeps the size of the command history.

    echo $HISTSIZE
    echo 'export HISTSIZE=2000' >> ~/.bashrc
    tail ~/.bashrc

    echo $HISTSIZE
    source ~/.bashrc
    echo $HISTSIZE
  4. One of the environment variables is PS1, which defines the prompt:

    echo $PS1
    ps1_old="$PS1"
    echo $ps1_old

    PS1="--> "
    ls -al

    PS1="\$ "
    ls -al

    PS1="\A \h \$ "
    ls -al

    "\A" displays the time of day and "\h" displays the host.

    PS1="<\u@\h \W>\$ "
    ls -al

    "\u" displays the user and "\W" displays the name of the current directory.

    To save this prompt for future sessions of the shell, we should append this line to ~/.bashrc:

    export PS1="<\u@\h \W>\$ "

    Restore the old prompt:

    PS1="<\u@\h \W>\$ "
    ls -al

    PS1="$ps1_old"
    ls -al